Keras CNN training and tuning on SageMaker
Trains a Keras MNIST CNN on SageMaker GPU instances, tunes hyperparameters and deploys the best model.
What it does
MNIST arrays are saved as NPZ files and uploaded to S3 as training and validation channels. A script-mode TensorFlow estimator runs a Keras CNN training script, first in local mode for one epoch and then on an ml.p3.2xlarge GPU instance. SageMaker automatic model tuning searches epochs, learning rate and batch size to maximise validation accuracy, and the best model is deployed to an ml.c5.large endpoint, tested and deleted.
What was built
- 01
Training script reads epochs, learning rate, batch size and SageMaker channel paths from argparse and environment variables.
- 02
CNN stacks two Conv2D layers, max pooling, dropout and dense layers with a 10-way softmax.
- 03
Local-mode run checks the script for one epoch before a 10-epoch ml.p3.2xlarge job.
- 04
HyperparameterTuner runs 10 jobs, two in parallel, maximising val_acc parsed from logs by regex.
- 05
Best tuned model is deployed to ml.c5.large, used for sample predictions, then the endpoint is deleted.
The pipeline, layer by layer
Four layers, one pipeline.
L1Data
- MNIST NPZ files
- S3 channels
L2Training
- Keras CNN script
- Script-mode estimator
L3Tuning
- Automatic model tuning
L4Serving
- ml.c5.large endpoint