Chapter 30: TensorFlow Example 2

What is “TensorFlow.js Example 2”?

Example 2 = training a convolutional neural network (CNN) on the MNIST handwritten digit dataset to classify images of digits 0–9.

  • Dataset: 60,000 training images + 10,000 test images (each 28×28 grayscale pixels)
  • Goal: Feed a new 28×28 image → model outputs “this is a 5” (or 3, 8, 9, etc.)
  • Model type: simple CNN (Conv2D + MaxPooling + Dense layers)
  • Output: 10 probabilities (one for each digit) → highest one wins

This is the first time you work with:

  • Image data (raw pixels → tensors)
  • Convolutional layers (learn edges, shapes, patterns)
  • Multi-class classification (10 possible answers)
  • Softmax + categorical crossentropy
  • High accuracy (97–99%) in the browser — feels like real AI

Why Example 2 Comes Right After Example 1

  • Example 1 → linear regression (1 input, 1 output, 2 parameters)
  • Example 2 → image classification (28×28×1 input, 10 outputs, thousands of parameters)
  • Same core pattern: data → model → compile → fit → predict
  • But now you see deep learning power — convolutions, pooling, dropout

Full Runnable Code for Example 2 (Browser – No Server Needed)

Save this as tfjs-example2.html and double-click to open in Chrome/Edge/Firefox.

HTML

What Happens When You Run This

  1. Click Train Model → data loads (60k images — takes 10–30 sec)
  2. Model builds → tfjs-vis Visor opens automatically (floating window)
  3. Training runs 6 epochs → you see:
    • Loss dropping (from ~2.3 → ~0.05–0.1)
    • Accuracy rising (from ~10% → 97–99%)
    • Validation curves (shows it generalizes)
  4. After ~1–3 min → accuracy usually 97.5–99%
  5. Draw any digit 0–9 on canvas (mouse) → click Predict My Drawing → model tells you the digit

Common Results You Should See

  • Final test accuracy: 97.5–99.2% (very good for this small CNN)
  • Visor graphs: smooth loss decrease, accuracy plateauing near 98–99%
  • Your own drawing: if you draw clearly → correct most times

Why This is Called “Example 2”

  • Example 1 → linear regression (1 neuron, regression)
  • Example 2 → convolutional neural network (multiple layers, classification)
  • Same training pattern (fit / fitDataset) → but now on real image data
  • Introduces Conv2D, MaxPooling2D, softmax, categoricalCrossentropy — building blocks of modern vision AI

In Hyderabad 2026, this Example 2 is still the second thing every student, developer, and job interviewee runs — because once you see handwritten digits recognized in your browser with 98%+ accuracy, you know you can build real apps (photo tagging, digit recognition in banking apps, medical image tools, etc.).

Understood the jump from Example 1 to Example 2? 🌟

Want next?

  • Improve this model to 99.2%+ accuracy?
  • Add confusion matrix in Visor after training?
  • Save/load this model & use it in another project?

Just tell me — next class is ready! 🚀

You may also like...

Leave a Reply

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