【发布时间】:2015-11-16 13:41:10
【问题描述】:
在 numpy 中,我们可以这样做:
x = np.random.random((10,10))
a = np.random.randint(0,10,5)
b = np.random.randint(0,10,5)
x[a,b] # gives 5 entries from x, indexed according to the corresponding entries in a and b
当我在 TensorFlow 中尝试类似的东西时:
xt = tf.constant(x)
at = tf.constant(a)
bt = tf.constant(b)
xt[at,bt]
最后一行给出了“Bad slice index tensor”异常。 TensorFlow 似乎不支持像 numpy 或 Theano 这样的索引。
有人知道是否有 TensorFlow 方法可以做到这一点(通过任意值索引张量)。我已经看过 tf.nn.embedding 部分,但我不确定它们是否可以用于此目的,即使可以,对于如此简单的事情来说,这也是一个巨大的解决方法。
(现在,我将来自x 的数据作为输入提供并在numpy 中进行索引,但我希望将x 放入TensorFlow 中以获得更高的效率)
【问题讨论】:
-
这个问题似乎正好解决了这个问题:github.com/tensorflow/tensorflow/issues/206
标签: python tensorflow