我有一个关于如何在TensorFlow中进行索引的基本问题。
在numpy中:
x = np.asarray([1,2,3,3,2,5,6,7,1,3])
e = np.asarray([0,1,0,1,1,1,0,1])
#numpy
print x * e[x]
我可以得到
[1 0 3 3 0 5 0 7 1 3]
我如何在TensorFlow中做到这一点?
x = np.asarray([1,2,3,3,2,5,6,7,1,3])
e = np.asarray([0,1,0,1,1,1,0,1])
x_t = tf.constant(x)
e_t = tf.constant(e)
with tf.Session():
????
谢谢!
幸运的是,TensorFlow支持您所要求的确切情况tf.gather()
:
result = x_t * tf.gather(e_t, x_t)
with tf.Session() as sess:
print sess.run(result) # ==> 'array([1, 0, 3, 3, 0, 5, 0, 7, 1, 3])'
该tf.gather()
运算是小于强大NumPy的先进索引:它仅支持其零维提取张量的全片。有人要求支持更一般的索引编制,并且此GitHub问题中对此进行了跟踪。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句