简体   繁体   English

如何在张量流中的张量中执行项目分配?

[英]How to perform item assignment in a tensor in tensorflow?

Let a tensor be a = [0,0,0,0,0,0,0,0] and another tensor be b = [1,3,0,5] , here I want a tensorflow operation to put 1 in the tensor a taking the position values from the tensor b . 假设张量为a = [0,0,0,0,0,0,0,0] ,另一个张量为b = [1,3,0,5] ,这里我想要一个张量流操作将1放入张量a从张量b获得位置值。 Hence the output tensor will be, [1,1,0,1,0,1,0,0] . 因此,输出张量将为[1,1,0,1,0,1,0,0] How to solve this problem? 如何解决这个问题呢?

What about this ? 那这个呢 ?

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))

The output is 输出是

[1 1 0 1 0 1 0 0] [1 1 0 1 0 1 0 0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM