I need to create a tf.Variable
with shape which is known only at the execution time.
I simplified my code to the following gist. I need to find in placeholder numbers which is greater than 4 and in the resultant tensor need to scatter_update the second item to 24 constant.
import tensorflow as tf
def get_variable(my_variable):
greater_than = tf.greater(my_variable, tf.constant(4))
result = tf.boolean_mask(my_variable, greater_than)
# result = tf.Variable(tf.zeros(tf.shape(result)), trainable=False, expected_shape=tf.shape(result), validate_shape=False) # doesn't work either
result = tf.get_variable("my_var", shape=tf.shape(my_variable), dtype=tf.int32)
result = tf.scatter_update(result, [1], 24)
return result
input = tf.placeholder(dtype=tf.int32, shape=[5])
created_variable = get_variable(input)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
result = sess.run(created_variable, feed_dict={input: [2, 7, 4, 6, 9]})
print(result)
I found few questions but they have no answers and didn't help me.