6

I have frequently seen the use of the symbols "None", "-1" and "?" to let the program dynamically define a tensor's dimension (such as the batch size), seemingly to the same effect:

x = tf.placeholder(tf.float32, [None, 48, 48, 3], name='InputData')

input = tf.reshape(input, [-1, input_size])

foo("bar", shape=(?,48), dtype=float32)

Are there any functional differences to using any one of these symbols over the other?

Thanks

3
  • Have you tried?
    – Timmy
    Aug 7, 2018 at 17:53
  • As far as I know the None keyword is the only one that's accepted by tf.placeholder, and reshape only accepts -1 as the new tensor shape which is confusing because in the tensorflow documentation both of these functions take a shape object as their second argument. I'm not so sure about the use case of the ? symbol, there's a similar post about that at stackoverflow.com/questions/40951602/… but they didn't mention whether None of -1 were acceptable inputs.
    – David Yu
    Aug 7, 2018 at 18:12
  • Ok thank you. I'm really sorry but I cannot answer you.
    – Timmy
    Aug 9, 2018 at 14:56

1 Answer 1

10

I think of it this way:

None means an unspecified dimension. So if for example you define a placeholder, you use None to say "this dimension can have any size".
A placeholder can have multiple None dimensions. This simply means multiple dimensions can be different size. Even the whole shape can be None to specify an unknown number of dimensions.

-1 is an instruction to Tensorflow to infer the size of the dimension on its own. In tf.reshape(input, [-1, input_size]), this means "reshape it such that the second dimension is input_size, and the first dimension is whatever is needed to match the total number of elements".
This does not necessarily mean that the dimension is unknown, as it is for None. If your input tensor has a known size of 10 elements, and you reshape to [-1, 2], Tensorflow is able to infer the full shape [5, 2].
-1 is purely for convenience. You could always write down the shape explicitly instead of letting Tensorflow infer it. None on the other hand is necessary for accepting variable-size tensors.
You can only ever have one -1 in a shape. Multiple ones don't make sense because it would be impossible to infer the shape. E.g. if you have 12 elements in a tensor, reshaping to [-1, -1, 2] is undefined -- should we do [3, 2, 2]? [2, 3, 2]? [6, 1, 2]?...

Finally, the question mark is just what Tensorflow uses to mark an "unknown" dimension when printing tensors and/or their shapes. The example you posted would actually produce a syntax error -- you cannot use question marks yourself. Causes of unknown dimensions can of course be placeholders with None dimensions, and often tensors that are defined in terms of placeholders (i.e. the result of some ops applied to them) will have unknown dimensions as well. Also some ops may not specify (parts of) their output shape which can also result in unknowns.

There are likely some more technicalities I missed here, but as a rule of thumb: Use None for placeholders and use -1 for reshape. This should cover the majority of use cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.