Ivan Talalaev
Ivan Talalaev

Reputation: 6494

TensorFlow create dynamic shape variable

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.

Upvotes: 1

Views: 455

Answers (1)

ffrank
ffrank

Reputation: 62

I had the same problem, stumbled upon the same unanswered questions and managed to piece together a solution for creating a variable with a dynamic shape at graph creation time. Note that the shape has to be defined before, or with the first execution of tf.Session.run(...).

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)
    zerofill = tf.fill(tf.shape(my_variable), tf.constant(0, dtype=tf.int32))
    # Initialize
    result = tf.get_variable(
        "my_var", shape=None, validate_shape=False, dtype=tf.int32, initializer=zerofill
    )
    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)

The trick is to create a tf.Variable with shape=None, validate_shape=False and hand over a tf.Tensor with unknown shape as initializer.

Upvotes: 1

Related Questions