Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one initialize a variable with tf.get_variable and a numpy value in TensorFlow?

I wanted to initialize some of the variable on my network with numpy values. For the sake of the example consider:

init=np.random.rand(1,2) tf.get_variable('var_name',initializer=init) 

when I do that I get an error:

ValueError: Shape of a new variable (var_name) must be fully defined, but instead was <unknown>. 

why is it that I am getting that error?

To try to fix it I tried doing:

tf.get_variable('var_name',initializer=init, shape=[1,2]) 

which yielded a even weirder error:

TypeError: 'numpy.ndarray' object is not callable 

I tried reading the docs and examples but it didn't really help.

Is it not possible to initialize variables with numpy arrays with the get_variable method in TensorFlow?

like image 919
Charlie Parker Avatar asked Jul 04 '16 18:07

Charlie Parker


People also ask

How do you initialize a variable in TensorFlow?

Variable initializers must be run explicitly before other ops in your model can be run. The easiest way to do that is to add an op that runs all the variable initializers, and run that op before using the model. You can alternatively restore variable values from a checkpoint file, see below.

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 does NumPy () do in TensorFlow?

Both in Pytorch and Tensorflow, the . numpy() method is pretty much straightforward. It converts a tensor object into an numpy. ndarray object.

What is the difference between tf variable and tf get_variable?

As far as I know, Variable is the default operation for making a variable, and get_variable is mainly used for weight sharing.


1 Answers

The following works, if you convert the constant NumPy array into a constant Tensor:

init = tf.constant(np.random.rand(1, 2)) tf.get_variable('var_name', initializer=init) 

The documentation for get_variable is a little lacking indeed. Just for your reference, the initializer argument has to be either a TensorFlow Tensor object (which can be constructed by calling tf.constant on a numpy value in your case), or a 'callable' that takes two arguments, shape and dtype, the shape and data type of the value that it's supposed to return. Again, in your case, you can write the following in case you wanted to use the 'callable' mechanism:

init = lambda shape, dtype: np.random.rand(*shape) tf.get_variable('var_name', initializer=init, shape=[1, 2]) 
like image 174
keveman Avatar answered Oct 07 '22 22:10

keveman