“Building a Neural Network for Handwritten Digit Recognition with TensorFlow”

🔹 Introduction

Handwritten digit recognition is a classic problem in machine learning. In this tutorial, we will build a simple Neural Network using TensorFlow to recognize digits (0–9). Don’t worry if you’re new — we’ll explain each step clearly!

🔹 Step 1: Importing Packages

First, we need to load the Python libraries. - NumPy: for calculations - Matplotlib: for graphs/images - TensorFlow: for building the neural network

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import matplotlib.pyplot as plt
  

🔹 Step 2: Activation Functions

Neural networks need activation functions to learn complex patterns.

  • ReLU (Rectified Linear Unit): Keeps positive values, makes model faster.
  • a = max(0, z)
  • Softmax: Converts final outputs into probabilities (which digit is most likely).

🔹 Step 3: Dataset

We use 5000 images of handwritten digits (each 20×20 pixels). Every image is converted into a 400-number vector. Each label (y) is a digit from 0 to 9.

X, y = load_data()
  

🔹 Step 4: Neural Network Architecture

The network we build looks like this:

  • Input Layer: 400 features (pixels)
  • Hidden Layer 1: 25 neurons, ReLU
  • Hidden Layer 2: 15 neurons, ReLU
  • Output Layer: 10 neurons (digits 0–9)

🔹 Step 5: Model Implementation

model = Sequential([
    tf.keras.Input(shape=(400,)),     
    Dense(25, activation='relu'), 
    Dense(15, activation='relu'),  
    Dense(10, activation='linear'), 
])
  

🔹 Step 6: Training the Model

We train the model using the Adam optimizer (for speed) and SparseCategoricalCrossentropy (for classification).

model.compile(
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    optimizer=tf.keras.optimizers.Adam(0.001),
)
history = model.fit(X, y, epochs=40)
  

🔹 Step 7: Making Predictions

After training, we test the model with new digits. Softmax helps us interpret results as probabilities.

prediction = model.predict(X[1015].reshape(1,400))
prediction_p = tf.nn.softmax(prediction)
print(np.argmax(prediction_p))
  

🔹 Step 8: Results & Error Analysis

✅ Training reduces loss (error) over time. ✅ Model predicts digits correctly most of the time. ❌ Some errors happen — more epochs or better models can help.

🔹 Conclusion

We successfully built a neural network for digit recognition using TensorFlow. Key takeaways:

  • ReLU helps networks learn complex patterns
  • Softmax turns outputs into probabilities
  • Neural networks can classify digits 0–9 effectively

👉 Next Steps: Try with MNIST (60,000 images), add dropout layers, or experiment with CNNs for higher accuracy.



Post a Comment

Previous Post Next Post