Pooling layers
The example creates a Keras Sequential model and adds layers in a fixed order.
Start with the layer sequence
The example is a small convolutional network, or convnet, built with Keras. Its first lesson is not training. It is learning to read the model definition: identify the model container, identify each layer, read the configuration attached to that layer, and follow the order in which the layers are added.
What do you think happens?
Before examining the individual arguments, what should you look for first in the model definition?
Reveal answer
Answer: The order of the model.add calls
Each model.add call adds the next layer to the Sequential stack. Reading those calls from top to bottom reveals the intended layer order.
A Sequential model is a linear stack of layers for building a simple neural network. In this example, model.add is the operation that extends the stack. Therefore, the order of the calls is part of the model itself: a layer added earlier appears earlier in the sequence, and a layer added later appears later.
Read a Conv2D layer
Conv2D is a two-dimensional convolutional layer used for image-processing tasks. In the source example, each Conv2D call specifies a number of filters and a kernel size. The first Conv2D call also specifies an activation argument and the input shape.
| Configuration value | What to identify in the example |
|---|---|
| Filters | The number of filters specified by the Conv2D call |
| Kernel size | The (3, 3) kernel size used by the Conv2D calls |
| Activation | The shown activation argument; it is included in the first Conv2D call and also used by the later Conv2D calls as described in the source |
| Input shape | An additional setting included by the first Conv2D call |
Configuration values to check when reading a Conv2D call
The important reading habit is to separate the layer type from its arguments. Conv2D tells you which kind of layer it is. The filter count and kernel size describe its stated configuration. The first call has extra information because it also includes the activation argument and the input shape.
Understand MaxPooling2D
MaxPooling2D is a two-dimensional max-pooling layer used for downsampling images. In the source example, both MaxPooling2D calls use the pool size (2, 2).
When reading a MaxPooling2D call, look for its pool-size argument rather than looking for a filter count or a kernel-size argument. In this example, the pool size is (2, 2) in both pooling calls. Its role in the layer sequence is downsampling, so it is distinct from the Conv2D layers that perform the convolutional stages.
Trace the complete definition
Reading the small convnet from top to bottom
Determine what to record while tracing the model definition.
1. Identify the container: Record that the example creates a Keras Sequential model. This tells you that the layers form a linear stack.
2. Read the first model.add call: Identify the first layer as Conv2D. Record its filter count, its (3, 3) kernel size, its activation argument, and its input shape.
3. Read the next model.add call: Identify the next layer as MaxPooling2D and record its (2, 2) pool size.
4. Continue one call at a time: For every later Conv2D call, check the filter count, the (3, 3) kernel size, and the activation argument. For each MaxPooling2D call, check the (2, 2) pool size.
5. Compare the pattern: The complete source definition alternates convolution and pooling. Use that pattern as a check, but confirm it against the actual model.add calls rather than assuming the pattern without reading.
The model can be understood by recording the layer type first and then checking only the configuration values that belong to that layer.
This trace gives you two independent checks. The first check is order: the calls should form the intended convolution-and-pooling sequence. The second check is configuration: Conv2D calls should be inspected for filters, (3, 3), and the shown activation argument, while MaxPooling2D calls should be inspected for (2, 2).
Avoid configuration mix-ups
Confusing the layer's position with its configuration.
Position comes from the order of model.add calls. Filters, kernel size, activation, input shape, and pool size are separate configuration details.
Fix:
Record the call order first, then read the arguments attached to each layer.Reading a MaxPooling2D call as though it were Conv2D.
The source identifies pool size as the required configuration for MaxPooling2D.
Fix:
For MaxPooling2D, check the pool size. In this example it is (2, 2).Swapping the two pair-valued settings.
The source assigns (3, 3) to the Conv2D kernel size and (2, 2) to the MaxPooling2D pool size.
Fix:
Name the layer before naming the pair: Conv2D uses kernel size (3, 3); MaxPooling2D uses pool size (2, 2).Forgetting the extra settings on the first Conv2D call.
The first Conv2D call also specifies an activation argument and the input shape.
Fix:
Give the first Conv2D call a separate check for activation and input shape.Assuming the intended pattern without checking every call.
A declaration can be debugged only by comparing the intended sequence with the actual model.add calls.
Fix:
Inspect each call from top to bottom, checking the layer type and then the matching configuration values.
Use a two-column reading method when debugging: write the layer order in one column and the layer-specific settings in the other. This prevents a layer's position from being mistaken for one of its arguments and makes a mismatch easier to locate.
Check your reading
Imagine you are given the small convnet definition and must review it without running it. Write down the checks you would perform for the first Conv2D call, one later Conv2D call, and one MaxPooling2D call.
Hints
- Start with the layer type and its position in the model.add sequence.
- For Conv2D, check filters, the (3, 3) kernel size, and the shown activation argument.
- For the first Conv2D call, also check the input shape.
- For MaxPooling2D, check the (2, 2) pool size.
- A Sequential model is a linear stack, and each model.add call contributes the next layer. Conv2D is the convolutional layer in the example; inspect its filter count, (3, 3) kernel size, and shown activation argument, with input shape also present on the first call. MaxPooling2D is the downsampling layer; both calls use pool size (2, 2). The safest way to read or debug the definition is to trace every call from top to bottom, identify the layer type first, and then check the configuration values that belong to that type.
Key Takeaways
- A Keras Sequential model represents a linear stack of layers.
- The model's order is revealed by tracing model.add calls from top to bottom.
- Conv2D calls use filter counts, a (3, 3) kernel size, and the shown activation argument; the first call also includes input shape.
- MaxPooling2D calls use a (2, 2) pool size and provide downsampling.
- Layer order and layer configuration are separate checks when reading or debugging the model.