【问题标题】:tensorflow ValueError: Shape must be rank 1 but is rank 2tensorflow ValueError:形状必须为 1 级,但为 2 级
【发布时间】:2016-11-02 04:19:08
【问题描述】:
import tensorflow as tf
x = [[1,2,3],[4,5,6]]
y = [0,1]
z = [1,2]
x = tf.constant(x)
y = tf.constant(y)
z = tf.constant(z)
m = x[y,z]

我期待的是m = [2,6]

我可以通过 theano 或 numpy 得到结果。如何使用 tensorflow 获得结果?

【问题讨论】:

  • 我认为这不是 tensorflow 问题,python 中的事件不起作用。如果您定义 x 是一个列表,并且您无法根据需要访问列表。将 x 定义为 numpy.array( [[1,2,3],[4,5,6]]) 就可以了。
  • 和numpy数组一样,我的评论有误。

标签: tensorflow deep-learning


【解决方案1】:

你会想使用tf.gather_nd

   slices = tf.gather_nd(x, [y, z])

希望这会有所帮助。

【讨论】:

  • 谢谢,我用 tf.pack() 和 tf.gather_nd() 得到结果。
  • 但是我得到 Gradient for gather_nd is not implemented 的错误。 T_T,不管怎样,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
  • 2019-05-10
  • 2020-10-17
  • 1970-01-01
  • 2018-05-30
相关资源
最近更新 更多