Livedocs
MNIST Digit Recognition
This notebook explores the MNIST dataset, building and training a simple neural network to recognize handwritten digits. It visually demonstrates data distribution, model training progress, and prediction accuracy using stunning Matplotlib and Plotly visualizations, including 3D dimensionality reduction with PCA to reveal hidden data structures.
2025-09-23 09:23:29.058549: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used. 2025-09-23 09:23:29.115630: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2025-09-23 09:23:31.256321: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
📞 Downloading MNIST dataset... Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11490434/11490434 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step
✨ Dataset loaded successfully! 📊 Training images: 60,000 📊 Test images: 10,000 🖼️ Image dimensions: 28 x 28 pixels 🔢 Unique digits: 10 (0-9)
📊 Training samples per digit: Digit 0: 5,923 samples Digit 1: 6,742 samples Digit 2: 5,958 samples Digit 3: 6,131 samples Digit 4: 5,842 samples Digit 5: 5,421 samples Digit 6: 5,918 samples Digit 7: 6,265 samples Digit 8: 5,851 samples Digit 9: 5,949 samples
Output Image image/png - dd6f9fe9-9ff7-4c26-87f5-1e6249f2b408
✨ Each row shows 10 different examples of the same digit! 🎨 Notice how each person writes numbers differently - that's what makes this challenging!
Output Image image/png - 69099f5b-29b7-4de7-a8eb-8561960f4efe
📊 Dataset Balance Analysis: • Total samples: 60,000 • Average per digit: 6,000 • Min samples: 5,421 (Digit 5) • Max samples: 6,742 (Digit 1) • Standard deviation: 339.5 🌟 This is a beautifully balanced dataset - perfect for machine learning!

🤖 Building Our AI "Brain" - A Simple Neural Network

What's a Neural Network? (ELI5)

Imagine your brain has millions of tiny helpers called neurons. When you see a handwritten "7", these helpers work together:

  1. Input Layer: Some helpers look at different parts of the image ("I see a vertical line!")
  2. Hidden Layer: Other helpers combine this info ("Vertical line + horizontal top = probably a 7!")
  3. Output Layer: Final helpers make the decision ("I'm 95% sure this is a 7!")

Our Simple Model Architecture

We're building a "shallow" neural network - think of it as a smart but simple brain:

  • Input: 28x28 pixel image (784 numbers)
  • Hidden Layer: 128 "smart helpers" (neurons)
  • Output: 10 possibilities (digits 0-9)

Why This Works

Just like how you learned to recognize numbers by seeing thousands of examples, our AI will:

  1. Look at 60,000 handwritten examples
  2. Learn patterns ("7s usually have this shape")
  3. Guess what new digits are
  4. Get better by learning from mistakes

Let's watch our AI baby learn to read! 📚✨

📋 Preparing the data... • Training data shape: (60000, 784) • Test data shape: (10000, 784) • Pixel values now range from 0 to 1 (normalized) 🏠 Building our AI brain... 🎨 Our Neural Network Architecture:
2025-09-23 09:27:27.502455: E external/local_xla/xla/stream_executor/cuda/cuda_platform.cc:51] failed call to cuInit: INTERNAL: CUDA error: Failed call to cuInit: UNKNOWN ERROR (303)
✨ Model ready! Let's count the parameters... • Total parameters (weights & biases): 101,770 • This AI has 101,770 tiny 'brain connections' to learn from!
🎯 Setting up training... 🏃 Starting training - watch our AI learn! This might take a few minutes... Epoch 1/10
422/422 ━━━━━━━━━━━━━━━━━━━━ 4s 6ms/step - accuracy: 0.8761 - loss: 0.4305 - val_accuracy: 0.9542 - val_loss: 0.1749
Epoch 2/10
422/422 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9428 - loss: 0.2020 - val_accuracy: 0.9677 - val_loss: 0.1208
Epoch 3/10
422/422 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9551 - loss: 0.1533 - val_accuracy: 0.9728 - val_loss: 0.1019
Epoch 4/10
422/422 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9642 - loss: 0.1222 - val_accuracy: 0.9752 - val_loss: 0.0843
Epoch 5/10
422/422 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9688 - loss: 0.1048 - val_accuracy: 0.9742 - val_loss: 0.0810
Epoch 6/10
422/422 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9727 - loss: 0.0914 - val_accuracy: 0.9780 - val_loss: 0.0754
Epoch 7/10
422/422 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9756 - loss: 0.0818 - val_accuracy: 0.9788 - val_loss: 0.0764
Epoch 8/10
422/422 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9772 - loss: 0.0724 - val_accuracy: 0.9780 - val_loss: 0.0730
Epoch 9/10
422/422 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9794 - loss: 0.0667 - val_accuracy: 0.9802 - val_loss: 0.0670
Epoch 10/10
422/422 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step - accuracy: 0.9814 - loss: 0.0599 - val_accuracy: 0.9800 - val_loss: 0.0668
✨ Training complete! Our AI has learned to read digits!
Output Image image/png - 29fc45b4-d89f-4fac-b329-1b9c939e2ada
🏆 Final Results: • Training Accuracy: 98.14% • Validation Accuracy: 98.00% • Our AI can now read handwritten digits with 98.0% accuracy!

🌌 Creating 3D Magic - How AI "Sees" Digits

What Are We About to See?

Our AI doesn't just memorize digits - it develops an internal "understanding" of them. Imagine if you could peek inside the AI's brain and see how it organizes different digits in its mind!

The Magic of Embeddings (ELI5)

Think of the AI's hidden layer as a 128-dimensional space (impossible to visualize). Each digit gets mapped to a point in this space. Similar-looking digits end up close together, while different ones are far apart.

The Problem: We can't see 128 dimensions! The Solution: Use special algorithms to squash this down to 3D while keeping the important relationships.

What to Look For

  • Clusters: Similar digits should form groups
  • Separation: Different digits should be far apart
  • Patterns: Some digits (like 6 and 9) might be closer together

The Algorithms We'll Use

  1. t-SNE: Great at preserving local neighborhoods
  2. PCA: Shows the main directions of variation
  3. UMAP: Preserves both local and global structure

Get ready for some mind-blowing 3D visualizations! 🚀✨

🌌 Creating 3D Magic - PCA Visualization of Digit Space... • Applying PCA to compress 784 pixels into 3D space...
• PCA complete! Explained variance: 23.5% • Created 3D coordinates for 1,500 digit samples
Output Image image/png - 0d0d49ba-003c-476a-9ba3-19ea8898b099
🌟 Absolutely stunning! This 3D visualization shows: • How digits cluster in high-dimensional space • Similar digits (like 6 & 8) appear closer together • The natural structure hidden in handwritten digits • These 3 dimensions capture 23.5% of the total variation!
🏆 Evaluating our trained AI on fresh test data...
✨ Final Test Results: • Test Accuracy: 97.83% • Test Loss: 0.0708 🔎 Making predictions on test set...
Output Image image/png - c427041b-ddee-4e38-9394-2bac111d9c3e
📊 Per-digit accuracy: Digit 0: 99.3% Digit 1: 99.4% Digit 2: 97.3% Digit 3: 98.0% Digit 4: 97.6% Digit 5: 97.6% Digit 6: 97.7% Digit 7: 97.5% Digit 8: 96.6% Digit 9: 97.1% 🎆 Amazing! Our simple AI achieved 97.8% accuracy! This means it correctly identifies handwritten digits 97+ times out of 100!
🌈 Final Showcase - Testing Our AI on Random Digits!
Output Image image/png - 89ff8e83-d276-4671-a97f-c29925053069
🏆 Sample Results: • Correct predictions: 12/12 • Sample accuracy: 100.0% • Green borders = correct, Red borders = incorrect ✨ Our AI journey is complete! From raw pixels to intelligent predictions. 🚀 Welcome to the amazing world of artificial intelligence and data visualization!