9

I'm getting the shape of a TensofFlow tensor as:

(?,)

This answer says that the ? means that the dimension is not fixed in the graph and it can vary between run calls.

What does the ? mean in conjuction with the trailing comma?

Documentation chapter and verse would be appreciated. I find syntax very difficult to google.

Tom Hale
  • 40,825
  • 36
  • 187
  • 242
  • 1
    Surely the `(X,)` indicates that the value is inside a container similar to a [`tuple`](https://docs.python.org/3/library/stdtypes.html#typesseq-tuple), with `X` as the only element? Eg, `print(tuple([2]))` will output `(2,)`. `(?,)` should therefore mean that the shape of your tensor is 1d, and its length is not fixed. Compare to output of numpy shape of 1d array – M.T Aug 22 '18 at 06:36
  • 1
    @grshankar I already linked to that question :) – Tom Hale Aug 22 '18 at 06:38

2 Answers2

6

The comma means that the dimension is represented as a 1-elem tuple instead an int.

Each tensor, when created, is by default a n-dim:

import tensorflow as tf
t = tf.constant([1, 1, 1])
s = tf.constant([[1, 1, 1],[2,2,2]])

print("0) ", tf.shape(t))
print("1) ", tf.shape(s))

0)  Tensor("Shape_28:0", shape=(1,), dtype=int32)
1)  Tensor("Shape_29:0", shape=(2,), dtype=int32)

However, you can reshape it to get a more "whole" shape (i.e. nXm / nXmXr... dim):

print("2) ", tf.reshape(t, [3,1]))
print("3) ", tf.reshape(s, [2,3]))

2)  Tensor("Reshape_12:0", shape=(3, 1), dtype=int32)
3)  Tensor("Reshape_13:0", shape=(2, 3), dtype=int32)
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
2

Use tf.shape(tensor)[0] to get a scalar tensor with the variable dimension. It is useful if you then need to reshape. tensor.get_shape().as_list()[0] generates None for shapes with (?, ... ). This is usually the position for the batch size when training models.

Reference: TF Issues

Lucho
  • 311
  • 2
  • 4