设一个张量为a = [0,0,0,0,0,0,0,0]
,另一个张量为b = [1,3,0,5 ]
,这里我想要一个 tensorflow 操作,将 1 放入张量 a
中,并从张量 b
中获取位置值。因此,输出张量将为[1,1,0,1,0,1,0,0]
。
如何解决这个问题呢?
最佳答案
这个怎么样?
a = tf.Variable([0,0,0,0,0,0,0,0])
b = tf.Variable([1,3,0,5])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
update = tf.scatter_update(a,
b,
tf.tile(tf.constant([1],
tf.int32),
b.shape))
print(update.eval(session=sess))
输出为
[1 1 0 1 0 1 0 0]
关于python - 如何在 tensorflow 中的张量中执行项目分配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52144159/