Lab build

Keras CNN training and tuning on SageMaker

Trains a Keras MNIST CNN on SageMaker GPU instances, tunes hyperparameters and deploys the best model.

Role
Builder, end to end
Group
MLOps
Services
AI & LLM systems · MLOps & cloud

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

  1. 01

    Training script reads epochs, learning rate, batch size and SageMaker channel paths from argparse and environment variables.

  2. 02

    CNN stacks two Conv2D layers, max pooling, dropout and dense layers with a 10-way softmax.

  3. 03

    Local-mode run checks the script for one epoch before a 10-epoch ml.p3.2xlarge job.

  4. 04

    HyperparameterTuner runs 10 jobs, two in parallel, maximising val_acc parsed from logs by regex.

  5. 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.

  1. L1Data

    • MNIST NPZ files
    • S3 channels
  2. L2Training

    • Keras CNN script
    • Script-mode estimator
  3. L3Tuning

    • Automatic model tuning
  4. L4Serving

    • ml.c5.large endpoint

Stack

  • Amazon SageMaker
  • SageMaker Python SDK
  • TensorFlow
  • Keras
  • Amazon S3
  • NumPy
  • matplotlib

Next lab build · 25 / 46

Wine quality modular ML pipeline

Config-driven pipeline that ingests, validates, transforms and trains an ElasticNet wine quality model.

Previous: Mobile price classifier on SageMaker

Need a system like this in production? Tell us about it.