Keras: How to get layer shapes in a Sequential model

If you want the output printed in a fancy way:

model.summary()

If you want the sizes in an accessible form

for layer in model.layers:
    print(layer.get_output_at(0).get_shape().as_list())

There are probably better ways to access the shapes than this. Thanks to Daniel for the inspiration.


According to official doc for Keras Layer, one can access layer output/input shape via layer.output_shape or layer.input_shape.

from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D


model = Sequential(layers=[
    Conv2D(32, (3, 3), input_shape=(64, 64, 3)),
    MaxPool2D(pool_size=(3, 3), strides=(2, 2))
])

for layer in model.layers:
    print(layer.output_shape)

# Output
# (None, 62, 62, 32)
# (None, 30, 30, 32)