1

I have a TensorFlow question about building TF array given a list of indices of its elements.

Say I have an index list with tf array

list_index_false = tf.constant([5,3])

I would like to build size 7 tf.array with booleans where only indices 5 and 3 are False while others are True such as below:
[True,True,True,False,True,False,True]


I tried following:

list_boolean=tf.fill([7],True))

Then tried to assign list_boolean[3]=False, list_boolean[5]=False, but tensorflow doesn't let me to assign. :(

How can I do that? Any other way?

Thank you

1 Answer 1

1

tf.scatter_update is specifically designed for usecases like this. Here is the code example:

import tensorflow as tf

list_index_false = tf.constant([5, 3])
list_boolean = tf.Variable(tf.fill([7], True))
list_boolean = tf.scatter_update(list_boolean, list_index_false, tf.fill(tf.shape(list_index_false), False))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(list_boolean))

Hope it helps!

Sign up to request clarification or add additional context in comments.

2 Comments

How can i possibly thank you! I have been in trouble for a week startin to hate Tensorflow.. You saved me.!
Glad to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.