Tensor With Unspecified Dimension In Tensorflow
Solution 1:
As Ishamael says, all tensors have a static shape, which is known at graph construction time and accessible using Tensor.get_shape()
; and a dynamic shape, which is only known at runtime and is accessible by fetching the value of the tensor, or passing it to an operator like tf.shape
. In many cases, the static and dynamic shapes are the same, but they can be different - the static shape can be partially defined - in order allow the dynamic shape to vary from one step to the next.
In your code normal_dist
has a partially-defined static shape, because w_shape
is a computed value. (TensorFlow sometimes attempts to evaluate
these computed values at graph construction time, but it gets stuck at tf.pack
.) It infers the shape TensorShape([Dimension(None), Dimension(None)])
, which means "a matrix with an unknown number of rows and columns," because it knowns that w_shape
is a vector of length 2, so the resulting normal_dist
must be 2-dimensional.
You have two options to deal with this. You can set the static shape as Ishamael suggests, but this requires you to know the shape at graph construction time. For example, the following may work:
normal_dist.set_shape([input_data.get_shape()[1], labels.get_shape()[1]])
Alternatively, you can pass validate_shape=False
to the tf.Variable
constructor. This allows you to create a variable with a partially-defined shape, but it limits the amount of static shape information that can be inferred later on in the graph.
Solution 2:
Similar question is nicely explained in TF FAQ:
In TensorFlow, a tensor has both a static (inferred) shape and a dynamic (true) shape. The static shape can be read using the
tf.Tensor.get_shape
method: this shape is inferred from the operations that were used to create the tensor, and may be partially complete. If the static shape is not fully defined, the dynamic shape of a Tensor t can be determined by evaluatingtf.shape(t)
.
So tf.shape()
returns you a tensor, will always have a size of shape=(N,)
, and can be calculated in a session:
a = tf.Variable(tf.zeros(shape=(2, 3, 4)))
with tf.Session() as sess:
print sess.run(tf.shape(a))
On the other hand you can extract the static shape by using x.get_shape().as_list()
and this can be calculated anywhere.
Solution 3:
The variable can have a dynamic shape. get_shape()
returns the static shape.
In your case you have a tensor that has a dynamic shape, and currently happens to hold value that is 4x3 (but at some other time it can hold a value with a different shape -- because the shape is dynamic). To set the static shape, use set_shape(w_shape)
. After that the shape you set will be enforced, and the tensor will be a valid initial_value
.
Post a Comment for "Tensor With Unspecified Dimension In Tensorflow"