Answer a question

I would like to access the layer size of all the layers in a Sequential Keras model. My code:

model = Sequential()
model.add(Conv2D(filters=32, 
               kernel_size=(3,3), 
               input_shape=(64,64,3)
        ))
model.add(MaxPooling2D(pool_size=(3,3), strides=(2,2)))

Then I would like some code like the following to work

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

.. but it doesn't. I get the error: AttributeError: 'Conv2D' object has no attribute 'get_shape'

Answers

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)
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐