首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >张量中的Tensorflow访问元--利用指数上的尾元

张量中的Tensorflow访问元--利用指数上的尾元
EN

Stack Overflow用户
提问于 2018-01-25 11:42:25
回答 1查看 345关注 0票数 0

如何使用张量索引访问tensorflow Tensor中的男高音元素,如下所示:

代码语言:javascript
复制
import tensorflow as tf
import numpy as np

# indexing in numpy [Working]
matrix = np.random.randint(0, 10, [100, 100])
indices = np.random.randint(0, 100, [1000, 100])
elements = matrix[indices[:, 0], indices[:, 1]]

# indexing in tensorflow [Not working]
tf_matrix = tf.constant(matrix, dtype=tf.int32)
tf_indices = tf.constant(indices, dtype=tf.int32)
tf_elements = tf_matrix[tf_indices[:, 0], tf_indices[:, 1]]  # Error

session = tf.Session()
session.run(tf_elements)

我知道这些错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError:形状必须为1级,但对于输入形状为:100、100、2,1000、2,1000、2,1000的“strided_slice_2”(op:“StridedSlice”)为2级。 ValueError:形状必须为1级,但对于输入形状为100、100、2、1000、2、1000、2,1000的“strided_slice_2”(op:“StridedSlice”),则为2级。

EN

回答 1

Stack Overflow用户

发布于 2018-01-25 11:52:46

代码语言:javascript
复制
tf_elements = tf.gather_nd(tf_matrix, tf_indices[:, 0:2])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48442034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档