yus*_*suf 5 python numpy tensorflow
让我们考虑一个 numpy 矩阵o:
如果我们想通过使用 numpy 来使用以下函数:
o[np.arange(x), column_array]
Run Code Online (Sandbox Code Playgroud)
我可以一次从一个 numpy 数组中获取多个索引。
我试图用 tensorflow 做同样的事情,但它不像我所做的那样工作。何时o是张量流张量;
o[tf.range(0, x, 1), column_array]
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
TypeError: can only concatenate list (not "int") to list
Run Code Online (Sandbox Code Playgroud)
我能做什么?
小智 5
您可以尝试tf.gather_nd(),如如何从 TensorFlow 中的 3-D 张量中选择行?这个帖子建议。这是从 matrix 获取多个索引的示例o。
o = tf.constant([[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]])
# [row_index, column_index], I don’t figure out how to
# combine row vector and column vector into this form.
indices = tf.constant([[0, 0], [0, 1], [2, 1], [2, 3]])
result = tf.gather_nd(o, indices)
with tf.Session() as sess:
print(sess.run(result)) #[ 1 2 10 12]
Run Code Online (Sandbox Code Playgroud)