The whole pipeline in one screen
This is the canonical deep-learning "Hello World": classify handwritten digits 0–9 from the MNIST dataset. What makes it worth your time isn't the dataset — it's that the full Keras workflow fits on one screen, and every project you ever build reuses these same six steps in the same order (the Code section is the complete script).
- Load —
keras.datasets.mnist.load_data()hands back train/test splits as NumPy arrays, no download plumbing. - Normalize — divide pixels by 255 so values land in [0, 1]. Skip this and training is sluggish or unstable; it's the single most common omission.
- Build — the Sequential stack from the last lessons: Flatten → Dense(relu) → Dropout → Dense(softmax).
- Compile — wire up the optimizer (
adam), the loss (sparse_categorical_crossentropy, because labels are plain integers), and the metric to watch. - Train —
fit()with avalidation_splitso you watch generalization, not just memorization. - Evaluate — score on the held-out test set you never trained on.
Five epochs gets you ~97–98% test accuracy. The loss choice in step 4 is the subtle one: sparse_categorical_crossentropy expects integer labels like 3, whereas categorical_crossentropy expects one-hot vectors like [0,0,0,1,0,...]. Pick the wrong one and Keras throws a shape error — which is exactly the failure mode the exercise asks you to trigger on purpose.