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?
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.
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.
Both in Pytorch and Tensorflow, the . numpy() method is pretty much straightforward. It converts a tensor object into an numpy. ndarray object.
As far as I know, Variable is the default operation for making a variable, and get_variable is mainly used for weight sharing.
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])
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