Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TensorFlow - Defining the shape of a variable dynamically, depending on the shape of another variable

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?

like image 878
Filipe Avatar asked Aug 17 '17 15:08

Filipe


People also ask

How many shapes does a tensor have in TensorFlow?

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!

What is a TensorFlow variable?

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.

What is a tensor variable in Python?

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.

How to create a matrix with 3 dimensions in TensorFlow?

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.


2 Answers

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)
like image 140
Max Avatar answered Nov 09 '22 23:11

Max


You can use x.get_shape():

y = tf.get_variable('y', shape=[x.get_shape()[0], 10])
like image 39
vijay m Avatar answered Nov 10 '22 00:11

vijay m