A convolutional neural network (CNN) learns image representations through convolutional layers. A practical classifier can process 28 × 28 video frames, use convolution and pooling to learn feature mappings, then use dense layers to assign one of 10 entities. section 3.2.2
A TensorFlow CNN can be assembled sequentially: name the input, add three convolutional layers with max pooling, flatten the resulting features, and finish with dense and softmax layers. Compile it with Adam, sparse categorical cross-entropy, and accuracy before training. section 9.2.1
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
This implementation shows the complete layer flow for a 28 × 28 single-channel input: Conv2D and pooling stages, flattening, a dense hidden layer, and 10-class softmax output. section 9.2.1
Useful training and model-variant choices include:
- Train locally with fit() and callbacks for learning-rate reporting, TensorBoard monitoring, checkpoints, and learning-rate decay. section 9.2.1
- Compare a basic CNN with batch-normalized and dropout variants; batch normalization standardizes layer pre-activations, while dropout randomly removes half of hidden units to reduce complexity. section 9.2.1
- Run distributed training for the three variants in parallel, persist their trained models, select a model, and send it to a serving step. section 9.4.1
Parallel CNN selection pipeline
A workflow downloads Fashion-MNIST into persistent storage, launches separate two-worker TFJobs for the basic, dropout, and batch-normalized CNNs, selects a trained model, and creates a TensorFlow inference service. section 9.4.1

















