Raba Poco
Raba Poco

Reputation: 23

Tensorflow - feeding examples with different length

Each of my training examples is a list with different length. I am trying to find a way to feed those examples into the graph. Below is my attempt to do so by creating a list whose elements are placeholders with unknown dimensions.

graph2 = tf.Graph()
with graph2.as_default():
    A = list ()
    for i in np.arange(3):
        A.append(tf.placeholder(tf.float32 ,shape = [None,None]))
    A_size = tf.shape(A)

with tf.Session(graph=graph2) as session:
  tf.initialize_all_variables().run()
  feed_dict = {A[0]:np.zeros((3,7)) ,A[1] : np.zeros((3,2)) , A[2] : np.zeros((3,2)) }
  print ( type(feed_dict))
  B = session.run(A_size ,feed_dict=feed_dict)
print type(B)

However I got the following error:

InvalidArgumentError: Shapes of all inputs must match: values[0].shape = [3,7] != values[1].shape = [3,2]

Any idea on how to solve it?

Upvotes: 2

Views: 2253

Answers (1)

Olivier Moindrot
Olivier Moindrot

Reputation: 28198

From the documentation of tf.placeholder:

shape: The shape of the tensor to be fed (optional). If the shape is not specified, you can feed a tensor of any shape.

You need to write shape=None instead of shape=[None, None]. With your code, Tensorflow doesn't know you are dealing with variable size input.

Upvotes: 2

Related Questions