DNN Handwritten Digit Classifier
A Deep Neural Network that recognizes handwritten digits (0-9) with 97%+ accuracy. Built using TensorFlow on the MNIST dataset to understand multi-layer networks and hierarchical feature learning.
Technologies
Project Overview
This is my second neural network project β a Deep Neural Network (DNN) that recognizes handwritten digits from the famous MNIST dataset. Building on my ANN knowledge, this project explores what happens when we add depth to neural networks.
The goal was to understand:
- How multiple layers learn hierarchical features
- Why non-linearity (ReLU) is essential
- How softmax enables multi-class classification
- The difference between ANN and DNN
π― The Problem
Given a 28Γ28 pixel grayscale image of a handwritten digit, classify it as one of 10 classes (0-9).
Dataset: MNIST
- 60,000 training images
- 10,000 test images
- 28Γ28 pixels, grayscale
π‘ The Solution
A 4-layer DNN architecture:
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
Layer-by-Layer Breakdown
| Layer | Purpose | Output Shape |
|---|---|---|
| Flatten | Convert 2D image β 1D vector | 784 |
| Dense(128, relu) | Learn simple patterns (edges) | 128 |
| Dense(64, relu) | Learn complex patterns (shapes) | 64 |
| Dense(10, softmax) | Output probabilities for 0-9 | 10 |
π§ Technologies Used
- Python 3.8+ β Programming language
- TensorFlow/Keras β Deep learning framework
- NumPy β Numerical computations
- MNIST Dataset β Built-in TensorFlow dataset
π Key Concepts Demonstrated
1. Data Normalization
x_train = x_train / 255.0 # Scale pixels to 0-1
2. ReLU Activation (Non-linearity)
activation='relu' # max(0, x)
3. Softmax for Multi-class Output
Dense(10, activation='softmax') # Probabilities sum to 1
4. Adam Optimizer
optimizer='adam' # Adaptive learning rate
5. Categorical Cross-Entropy Loss
loss='sparse_categorical_crossentropy' # For integer labels
π Results
| Metric | Value |
|---|---|
| Training Accuracy | ~98% |
| Validation Accuracy | ~97% |
| Test Accuracy | ~97-98% |
| Epochs | 5 |
| Training Time | ~30 seconds |
π§ What Each Layer Learns
Flatten β Raw pixel values
Dense(128) β Edges, simple curves
Dense(64) β Digit parts (loops, lines)
Dense(10) β Final classification probabilities
This is hierarchical feature learning β building complex understanding from simple patterns!
π What This Project Taught Me
- Depth enables complexity β more layers = more abstract features
- Non-linearity is essential β ReLU allows learning complex patterns
- Softmax outputs probabilities β perfect for classification
- Normalization matters β raw pixels hurt training stability
- Adam > SGD for most deep learning tasks
- Overfitting happens β regularization is important
π Links
- GitHub Repository β Full source code
- Blog Post: From ANN to DNN β Detailed explanation
This project is part of my AI/ML learning journey. Next up: Convolutional Neural Networks for image classification!