Tf.get_variable does not accept tensors for shape

It looks like it tf.get_variable

doesn't take shape Tensor

for a shape, only int

. This is not like tf.Variable

that can take Tensors

. Is this correct, and if so, is there a workaround tf.get_variable

to work when the form is Tensor

? I do not want to run session

at this stage of the plotting as it will complicate many things downstream.

I am trying to use cuDNN bindings for RNNs, which involves calling params_size()

on a model created with CudnnLSTM

. This returns the number of parameters to store in the parameter buffer as Tensor

, which is then used to create a variable that contains the parameters. Instead of using tf.Variable

, with all its caveats, I would like to use tf.get_variable

to store the parameter values ​​so that I can easily initialize them using all the different compatible initializers, tf.get_variable

etc. All new cars are built around tf.get_variable

, and therefore going back to low level is tf.Variable

very cumbersome. Seems like a very strange restriction tf.get_variable

not to accept Tensors

when tf.Variable

does.

+3


source to share


1 answer


A workaround is to provide an initializer that is shaped but dynamic. For example, instead of

s = tf.placeholder(tf.int32, shape=())
init = tf.random_normal_initializer()
tf.get_variable('foo', shape=(s,), initializer=init) # error, shape cannot be a Tensor

      

would you use



s = tf.placeholder(tf.int32, shape=())
init = tf.random_normal((s,))
tf.get_variable('foo', initializer=init, validate_shape=False)

      

Note, however, that variable initialization is now tricky. The call tf.global_variables_initializer()

will try to initialize 'foo'

, so you need to make sure that either it can be initialized (supply some values ​​if necessary), or it can initialize the initialization yourself.

Another workaround is to initialize your variable to any value (say a scalar of zero) and then the tf.assign

value with the desired shape when the shape is known. It's a little easier to work with during initialization, as the original value is discarded after fir assign

, so this might be a good alternative.

0


source







All Articles