Say I have a certain the Tensor x
whose dimensions are not defined upon graph initialization.
I can get its shape using:
x_shape = tf.shape(input=x)
Now if I want to create a variable based on the values defined in x_shape
using:
y = tf.get_variable(variable_name="y", shape=[x_shape[0], 10])
I get an error, since the values passed to the argument shape must be int
and not Tensor
. How can I create such a dynamically shaped variable without using placeholders?
Now, here is the most important piece of this article: Tensors in TensorFlow have 2 shapes: The static shape AND the dynamic shape! Tensor in TensorFlow has 2 shapes!
Was this helpful? A TensorFlow variable is the recommended way to represent shared, persistent state your program manipulates. This guide covers how to create, update, and manage instances of tf.Variable in TensorFlow. Variables are created and tracked via the tf.Variable class.
A variable looks and acts like a tensor, and, in fact, is a data structure backed by a tf.Tensor. Like tensors, they have a dtype and a shape, and can be exported to NumPy. Shape: (2, 2) DType: <dtype: 'float32'> As NumPy: [ [1. 2.] [3. 4.]] Most tensor operations work on variables as expected, although variables cannot be reshaped.
The matrix has 2 rows and 2 columns filled with values 1, 2, 3, 4. A matrix with 3 dimensions is constructed by adding another level with the brackets. The matrix looks like the picture two. When you print tensor, TensorFlow guesses the shape. However, you can get the shape of the tensor with the TensorFlow shape property.
I'm running out of time so this is quick and dirty, but maybe it helps you to get to your solution... It's based on this (dynamic size for tf.zeros) but extends the idea to tf.Variables. Since your variable needs to be initialized anyway - I choose 0s...
import tensorflow as tf
I1_ph = tf.placeholder(name = "I1",shape=(None,None,None),dtype=tf_dtype)
zerofill = tf.fill(tf.shape(I1_ph), 0.0)
myVar = tf.Variable(0.0)
updateMyVar = tf.assign(myVar,zerofill,validate_shape=False)
res, = sess.run([updateMyVar], { I1_ph:np.zeros((1,2,2)) } )
print ("dynamic variable shape",res.shape)
res, = sess.run([updateMyVar], { I1_ph:np.zeros((3,5,2)) } )
print ("dynamic variable shape",res.shape)
You can use x.get_shape()
:
y = tf.get_variable('y', shape=[x.get_shape()[0], 10])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With