When using Python Tensorflow input shape (53,))... what's going on with this comma?

Viewed 496

I'm confused by the following line of code:

input_img = Input(shape=(53,))

I have a batch of 52 images but how can a tuple have nothing after the comma? What does this mean?

2 Answers

The function Input excepts a tuple for the argument shape

Using the comma allows you to define a tuple with a single item. If you simply used (53) or 53, it would be interpreted as an integer:

type( 53 )
<class 'int'>
type( (53) )
<class 'int'>
type( (53,) )
<class 'tuple'>

This is because the simple brackets are used in computations hence cannot be parsed a tuples:

(53) + 2 # would raise an error if (53) was a tuple
(53 + 1)*2 # would also raise an error if (53+1) was a tuple

So in order to define a tuple with a single item, you have to add the comma: (53,)

It is the same as a scalar, but is interpreted as a shape.

If you are confused, you can look at how list can abuse this too, for example:

print([
       0,
       1,
       2,  # <- This comma is unnecessary but won't break your code
])

[0, 1, 2]

Related