在TensorFlow中每行索引

时间:2016-11-14 18:47:06

标签: tensorflow deep-learning

我有一个矩阵:

col_indices = 

[[0 1]

 [1 2]

 [2 3]]

对于每一行,我想使用列索引选择一些元素:

row_indices = 

[[0 0]

 [1 1]

 [2 2]]

在Numpy中,我可以创建行索引:

params[row_indices, col_indices]

并执行tf_params = tf.constant(params) tf_col_indices = tf.constant(col_indices, dtype=tf.int32) tf_row_indices = tf.constant(row_indices, dtype=tf.int32) tf_params[row_indices, col_indices]

在TenforFlow中,我这样做了:

ValueError: Shape must be rank 1 but is rank 3

但是出现了一个错误:

{{1}}

这是什么意思?我该如何正确地进行这种索引?

谢谢!

1 个答案:

答案 0 :(得分:0)

张量等级(有时称为顺序或度数或n维)是张量的维数。例如,以下张量(定义为Python列表)的等级为2:

t = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

二级张量是我们通常认为的矩阵,一级张量是一个矢量。对于二级张量,您可以使用语法t [i,j]访问任何元素。对于三级张量,您需要使用t [i,j,k]来寻址一个元素。有关详细信息,请参阅this

ValueError: Shape must be rank 1 but is rank 3表示您正在尝试创建一个3张量(数字的多维数据集)而不是向量。

要了解如何声明不同形状的张量常数,您可以看到this