text
stringlengths
0
4.99k
In general, it's a recommended best practice to always specify the input shape of a Sequential model in advance if you know what it is.
A common debugging workflow: add() + summary()
When building a new Sequential architecture, it's useful to incrementally stack layers with add() and frequently print model summaries. For instance, this enables you to monitor how a stack of Conv2D and MaxPooling2D layers is downsampling image feature maps:
model = keras.Sequential()
model.add(keras.Input(shape=(250, 250, 3))) # 250x250 RGB images
model.add(layers.Conv2D(32, 5, strides=2, activation="relu"))
model.add(layers.Conv2D(32, 3, activation="relu"))
model.add(layers.MaxPooling2D(3))
# Can you guess what the current output shape is at this point? Probably not.
# Let's just print it:
model.summary()
# The answer was: (40, 40, 32), so we can keep downsampling...
model.add(layers.Conv2D(32, 3, activation="relu"))
model.add(layers.Conv2D(32, 3, activation="relu"))
model.add(layers.MaxPooling2D(3))
model.add(layers.Conv2D(32, 3, activation="relu"))
model.add(layers.Conv2D(32, 3, activation="relu"))
model.add(layers.MaxPooling2D(2))
# And now?
model.summary()
# Now that we have 4x4 feature maps, time to apply global max pooling.
model.add(layers.GlobalMaxPooling2D())
# Finally, we add a classification layer.
model.add(layers.Dense(10))
Model: "sequential_6"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 123, 123, 32) 2432
_________________________________________________________________
conv2d_1 (Conv2D) (None, 121, 121, 32) 9248
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 40, 40, 32) 0
=================================================================
Total params: 11,680
Trainable params: 11,680
Non-trainable params: 0
_________________________________________________________________
Model: "sequential_6"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 123, 123, 32) 2432
_________________________________________________________________
conv2d_1 (Conv2D) (None, 121, 121, 32) 9248
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 40, 40, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 38, 38, 32) 9248
_________________________________________________________________
conv2d_3 (Conv2D) (None, 36, 36, 32) 9248
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 12, 12, 32) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 10, 10, 32) 9248
_________________________________________________________________
conv2d_5 (Conv2D) (None, 8, 8, 32) 9248
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 4, 4, 32) 0
=================================================================
Total params: 48,672
Trainable params: 48,672
Non-trainable params: 0
_________________________________________________________________
Very practical, right?
What to do once you have a model
Once your model architecture is ready, you will want to:
Train your model, evaluate it, and run inference. See our guide to training & evaluation with the built-in loops
Save your model to disk and restore it. See our guide to serialization & saving.
Speed up model training by leveraging multiple GPUs. See our guide to multi-GPU and distributed training.
Feature extraction with a Sequential model
Once a Sequential model has been built, it behaves like a Functional API model. This means that every layer has an input and output attribute. These attributes can be used to do neat things, like quickly creating a model that extracts the outputs of all intermediate layers in a Sequential model:
initial_model = keras.Sequential(
[
keras.Input(shape=(250, 250, 3)),
layers.Conv2D(32, 5, strides=2, activation="relu"),
layers.Conv2D(32, 3, activation="relu"),
layers.Conv2D(32, 3, activation="relu"),
]
)
feature_extractor = keras.Model(
inputs=initial_model.inputs,
outputs=[layer.output for layer in initial_model.layers],
)
# Call feature extractor on test input.
x = tf.ones((1, 250, 250, 3))
features = feature_extractor(x)
Here's a similar example that only extract features from one layer: