1

I am having hard time due to dynamic and static shapes in tf.

I have

shape=tf.shape(net)
s1=tf.cast(shape[2],tf.int32)
s2=tf.cast(shape[2]/2,tf.int32)
a0=tf.random_normal([s1,s2],mean=0.,stdev=1.)
b0 = tf.get_variable(some_name, initializer=a0)

I get the error:

ValueError: initial_value must have a shape specified:

for line b0=... .Then, I added the shape information:

b0 = tf.get_variable(some_name, initializer=a0,shape=[s1,s2])

Now I get the error:

If initializer is a constant, do not specify shape.

I realized, it has probably something to do with it being dynamic shape. So, I went back and changed to

shape = net.get_shape().as_list()

Now, I get the error:

ValueError: None values not supported.

in line corresponding to assignment of cast to s1.

I feel like I am running around in circles. How does one solve this?

I have gone through: How to understand static shape and dynamic shape in TensorFlow?

Dinesh
  • 1,555
  • 1
  • 16
  • 18

1 Answers1

1

You need to specify validate_shape=False in the argument of tf.get_variable, e.g

init = tf.random_normal((s1, s2))
tf.get_variable(name, initializer=init, validate_shape=False)
P-Gn
  • 23,115
  • 9
  • 87
  • 104
  • I have to apply fully connected layer after some operations on this. That throws the error: The last dimension of the inputs to `Dense` should be defined. Found `None`. I think I will have to hardcode both image size and batch size for now for a temporary solution. – Dinesh Jul 10 '17 at 08:01
  • 1
    @edit I think you are having a new problem beyond dynamic variable shape initialization that may be worth explaining in length in a new question. – P-Gn Jul 10 '17 at 08:51