Chapter 32: Example 2 Model

What is the “Example 2 Model”?

Example 2 Model = a small but powerful convolutional neural network (CNN) designed specifically for classifying 28×28 grayscale handwritten digits (MNIST dataset).

The code that defines it (this is the heart of every beginner TensorFlow.js MNIST tutorial):

JavaScript

This is the classic Example 2 Model — almost every tutorial uses almost exactly this architecture (sometimes minor variations like more/less filters or epochs).

Why This Exact Model Architecture?

It’s a deliberately simple yet effective CNN that teaches the core ideas without overwhelming you:

Layer Type Purpose (what it learns) Why we need it Output shape after this layer (batch ignored)
Conv2D (32 filters) Learn basic edges, lines, curves in small 3×3 patches Detects simple patterns (horizontal/vertical lines) [26, 26, 32]
MaxPooling2D (2×2) Reduce size, keep strongest signals, translation invariance Makes model smaller + more robust to small shifts [13, 13, 32]
Conv2D (64 filters) Learn more complex shapes (corners, loops, intersections) Combines simple edges into digit parts [11, 11, 64]
MaxPooling2D (2×2) Further reduce size, focus on important features Reduces computation, prevents overfitting [5, 5, 64]
Flatten Convert 3D feature map to 1D vector Prepare for dense classification layers [1600] (5×5×64 = 1600)
Dense (128 units) Combine high-level features into decisions Learn digit-specific combinations [128]
Dropout (0.2) Randomly ignore 20% neurons during training Prevent overfitting, force robust learning [128]
Dense (10 units) Final classification — 10 possible digits Output probabilities for each class [10]

Total Trainable Parameters (Roughly)

  • Conv1: 3×3×1×32 + 32 biases ≈ 320
  • Conv2: 3×3×32×64 + 64 biases ≈ 18,496
  • Dense 128: 1600×128 + 128 biases ≈ 204,928
  • Dense 10: 128×10 + 10 biases ≈ 1,290

Total ≈ 225,000 parameters — small by 2026 standards, but enough to reach 97–99% accuracy on MNIST.

Full Runnable Code for Example 2 Model (with Training & Prediction)

HTML

Final Teacher Summary

Example 2 Model = the small CNN used in the second TensorFlow.js tutorial — 2 convolutional blocks + pooling + dense layers — trained to recognize handwritten digits 0–9 from 28×28 images.

  • Conv layers → learn edges → shapes → digit parts
  • Pooling → reduce size, add shift invariance
  • Dense + softmax → final decision (10 classes)
  • Reaches 97–99% accuracy in browser

This model teaches you real deep learning — convolutions, feature hierarchy, classification, and why CNNs beat simple dense networks on images.

Got the full picture? 🌟

Want next?

  • Improve it to 99.2%+?
  • Visualize learned filters (what edges it detects)?
  • Convert this model to TensorFlow Lite / tfjs for mobile/web app?

Just say — next class is ready! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *