A convolutional layer applies learned filters to image-like inputs and produces feature mappings that later layers can use for classification. In a CNN, several convolutional layers are commonly interleaved with pooling before flattening and dense classification. section 3.2.2 section 9.2.1
A practical convolutional-layer workflow typically follows this structure:
- Provide an image-shaped input, such as a 28 × 28 frame. section 9.2.1
- Stack convolutional layers to learn increasingly useful feature mappings. section 3.2.2
- Use max-pooling layers to reduce intermediate representations before flattening and dense classification. section 9.2.1
- Compile and train the complete model with an optimizer, loss function, metrics, and callbacks for monitoring or checkpointing. section 9.2.1
Three-layer TensorFlow CNN
A Sequential model can apply Conv2D three times: 32 filters with a 3 × 3 kernel, then 64 filters twice. Max-pooling follows the first two convolutional layers; Flatten and Dense layers produce the final classification.
Listing 9.4 Defining the basic CNN model
def build_and_compile_cnn_model():
print("Training CNN model")
model = models.Sequential()
model.add(layers.Input(shape=(28, 28, 1), name='image_bytes'))
model.add(
layers.Conv2D(32, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.summary()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
Browse the complete TensorFlow definition to inspect the convolutional-layer sequence, tensor shapes, pooling stages, and compilation settings. section 9.2.1













