8

I tried creating a tf.Variable with a dynamic shape. The following outlines the problem.

Doing this works.

init_bias = tf.random_uniform(shape=[self.config.hidden_layer_size, tf.shape(self.question_inputs)[0]])

However, when i try to do this:

init_bias = tf.Variable(init_bias)

It throws the error ValueError: initial_value must have a shape specified: Tensor("random_uniform:0", shape=(?, ?), dtype=float32)

Just come context (question input is a placeholder which dynamic batch ):

self.question_inputs = tf.placeholder(tf.int32, shape=[None, self.config.qmax])

It seems like putting a dynamic value into random uniform gives shape=(?,?) which gives an error with tf.Variable.

Thanks and appreciate any help!

1 Answer 1

11

This should work:

init_bias = tf.Variable(init_bias,validate_shape=False)

If validate_shape is False, tensorflow allows the variable to be initialized with a value of unknown shape.

However, what you're doing seems a little strange to me. In tensorflow, Variables are generally used to store weights of a neural net, whose shape remains fixed irrespective of the batch size. Variable batch size is handled by passing a variable length tensor into the graph (and multiplying/adding it with a fixed shape bias Variable).

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

2 Comments

I found an use case in preloading data: tensorflow.org/programmers_guide/reading_data#preloaded_data. You might want to feed different datasets of different sizes.
I still get an error InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype int32 [[Node: Placeholder = Placeholder[dtype=DT_INT32, shape=[], device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

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.