Variable size tensor flow constant

I have a variable batch size, so all my inputs are of the form

tf.placeholder(tf.float32, shape=(None, ...) 

to take the size of the variable. However, how can you create a constant value with a variable batch size? The problem is this line:

 log_probs = tf.constant(0.0, dtype=tf.float32, shape=[None, 1]) 

This gives me an error:

 TypeError: unsupported operand type(s) for *: 'NoneType' and 'int' 

I am sure that it is possible to initialize a constant tensor with a variable batch size, how can I do this?

I also tried the following:

 tf.constant(0.0, dtype=tf.float32, shape=[-1, 1]) 

I get this error:

 ValueError: Too many elements provided. Needed at most -1, but received 1 
+6
source share
1 answer

A tf.constant() has a fixed size and value when plotting, so it is probably not suitable for your application.

If you are trying to create a tensor with a dynamic size and the same (constant) value for each element, you can use tf.fill() and tf.shape() to create a tensor of the corresponding shape. For example, to create a tensor t that has the same shape as input , and a value of 0.5 :

 input = tf.placeholder(tf.float32, shape=(None, ...)) # `tf.shape(input)` takes the dynamic shape of `input`. t = tf.fill(tf.shape(input), 0.5) 

As Yaroslav mentions in his comment , you can also use (NumPy-style) to avoid materializing the tensor with a dynamic form. For example, if input has the form (None, 32) , and t has the form (1, 32) , then calculating tf.mul(input, t) will translate t in the first dimension in accordance with the form input .

+13
source

All Articles