简体   繁体   中英

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 . Hence the output tensor will be, [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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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