Search code examples
pythonpython-3.xtensorflowkeraskeras-layer

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


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?


Solution

  • 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,)