我正在寻找一种(最佳方法)在给定位置的张量中放置一组值;或涉及张量,值列表和索引列表的操作,并输出相同的结果(即没有可变变量)。
NumPy中的等效代码为:
inds = np.array([1, 3, 5])
values = np.array([.5, .6, .1])
output = np.zeros((10,))
# Here is the line that I want to reproduce in TF
output[inds] = values
我已经找到了如何针对单个值(即output[inds] = 1
)执行此操作,但是我没有找到针对值列表的任何解决方案。
答案 0 :(得分:1)
import tensorflow as tf
inds = tf.constant([1, 3, 5])
values = tf.constant([.5, .6, .1])
# Add one dimension to indices and scatter
output = tf.scatter_nd(tf.expand_dims(inds, 1), values, (10,))
with tf.Session() as sess:
print(sess.run(output))
输出:
[0. 0.5 0. 0.6 0. 0.1 0. 0. 0. 0. ]