Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow cannot initialize tf.Variable for dynamic batch size

Tags:

tensorflow

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!

like image 941
op10no4 Avatar asked Oct 14 '16 13:10

op10no4


People also ask

How do you initialize a variable in tf?

To initialize a new variable from the value of another variable use the other variable's initialized_value() property. You can use the initialized value directly as the initial value for the new variable, or you can use it as any other tensor to compute a value for the new variable.

How do you initialize a TensorFlow variable in a matrix?

First, remember that you can use the TensorFlow eye functionality to easily create a square identity matrix. We create a 5x5 identity matrix with a data type of float32 and assign it to the Python variable identity matrix. So we used tf. eye, give it a size of 5, and the data type is float32.

What is tf Get_variable?

The function tf. get_variable() returns the existing variable with the same name if it exists, and creates the variable with the specified shape and initializer if it does not exist.

Is tf variable trainable?

To make this easier, the variable constructor supports a trainable=<bool> parameter. tf. GradientTape watches trainable variables by default: with tf.


1 Answers

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).

like image 131
user1523170 Avatar answered Sep 19 '22 16:09

user1523170