user5538922
user5538922

Reputation:

It seems inconsistent the ways tensorflow allows me to specify variable length dimension

I'm a novice to tensorflow. I was practicing coding with this tutorial code. Most of all the code made sense to me but at some points I got stuck.

import tensorflow as tf
x = tf.placeholder("float", [None, n_steps, n_input])
x = tf.transpose(x, [1, 0, 2])
x = tf.reshape(x, [-1, n_input])

With tf.placholder function I had to specify variable length dimesion with None. But with tf.reshape I had to use -1, not None. In documentation for the two functions, both of the pertaining arguments have the name shape. So I am feeling lost here. Do they really have different meanings? Or is it just a small design mistake of the tensorflow developers?

Upvotes: 0

Views: 43

Answers (1)

rmeertens
rmeertens

Reputation: 4451

You can understand it this way: in a placeholder the value "None" indicates: "could be any value". Like in your case: you have a batch size that can be anything. In a reshape function, -1 indicates "whatever value is remaining to make this shape work". In you case, your x gets the shape (batch*n_steps), as this is the shape x needs to be to fit the same data in a matrix.

Interesting note: you CAN use multiple None values in a placeholder (to indicate any batch size, any width and height of an image)... But you can't use multiple -1 values in a reshape function!

Upvotes: 1

Related Questions