Understanding the Keras layer input shapes

When creating a sequential model using Keras, we have to specify only the shape of the first layer. The number of expected values in the shape tuple depends on the type of the first layer. I have made a list of layers and their input shape parameters.

Batch size

(Almost) every kind of layer has the batch size parameter as the first elements of the input_shape tuple, but we usually don’t specify it as a part of the input definition. We do it later, during training, so I am going to skip the batch size in my examples.

Layer input shape parameters


The actual shape depends on the number of dimensions. In the case of a one-dimensional array of n features, the input_shape looks like this (batch_size, n). As I mentioned before, we can skip the batch_size when we define the model structure, so in the code, we write:

keras.layers.Dense(32, activation='relu', input_shape=(16,))

Would you like to help fight youth unemployment while getting mentoring experience?

Develhope is looking for tutors (part-time, freelancers) for their upcoming Data Engineer Courses.

The role of a tutor is to be the point of contact for students, guiding them throughout the 6-month learning program. The mentor supports learners through 1:1 meetings, giving feedback on assignments, and responding to messages in Discord channels—no live teaching sessions.

Expected availability: 15h/week. You can schedule the 1:1 sessions whenever you want, but the sessions must happen between 9 - 18 (9 am - 6 pm) CEST Monday-Friday.

Check out their job description.

(free advertisement, no affiliate links)


Surprisingly, the convolutional layer used for images needs four-dimensional input. As usual, the first parameter is the batch size and (as usual) we skip it. Next, are two parameters that denote the number of pixels of the image. As the last parameter, we put the number of channels. In case of a standard RGB image, the number of channels is 3.

It is also possible to specify the number of channels before the size. To do this, we also have to set the data_format to “channel_first.”

keras.layers.Conv2D(128, kernel_size=3, activation='relu', input_shape=(64,64,3)) #channel_last (the default style)

# or

keras.layers.Conv2D(128, kernel_size=3, activation='relu', input_shape=(3,64,64), data_format='channel_first')


In the case of LSTM, we have three parameters. The implicit batch size, which we don’t define (as usual), the parameter that denotes the number of timesteps (in this example, 4), and the number of features of every timestep (16).


Note that, I also had to specify the number of units (the number 8 in the first parameter). That is the size of the output. It is going to be used as the input size of the next layer.


Very similar to Conv2d. We put the additional time parameter after the batch size (so it is always the first one in the tuple, even if “channel_first” parameter is used, in that case, the channel is the second parameter). In this example, 4 denotes the number of timesteps.

keras.layers.ConvLSTM2D(filters=128, kernel_size=3, activation='relu', input_shape=(4,64,64,3)) #channel_last (the default style)

# or

keras.layers.ConvLSTM2D(filters=128, kernel_size=3, activation='relu', input_shape=(4,3,64,64), data_format='channel_first')

Remember to share on social media!
If you like this text, please share it on Facebook/Twitter/LinkedIn/Reddit or other social media.

If you want to contact me, send me a message on LinkedIn or Twitter.

Bartosz Mikulski
Bartosz Mikulski * MLOps Engineer / data engineer * conference speaker * co-founder of Software Craft Poznan & Poznan Scala User Group

Subscribe to the newsletter and get access to my free email course on building trustworthy data pipelines.