Skip to content

innocentgodson/PyTorch-MNIST-digit-recognition-model

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 

Repository files navigation

PyTorch-MNIST-digit-recognition-model

This code trains a fully connected neural network using PyTorch on the MNIST dataset to recognize handwritten digits (0–9).

Imports

image

Imports PyTorch, neural network tools, data loading utilities, and dataset + transformation tools.

Downloading and Transforming the MNIST Dataset

image

Downloads the MNIST dataset (handwritten digits).

Converts the 28×28 grayscale images into normalized PyTorch tensors (values between 0 and 1).

Data Loaders

image

Prepares batches of data (64 images per batch) for efficient training and testing.

Data Inspection

image

Confirms dataset sizes and inspects shape of image tensors.

X shape: [64, 1, 28, 28] → 64 images, 1 channel, 28×28 size.

Device Selection

image

Selects GPU (NVIDIA or Apple Silicon) if available, otherwise defaults to CPU.

Defining the Neural Network

image

A fully connected (dense) neural network with:

Input layer: 784 features (flattened 28×28 image)

2 hidden layers: 512 neurons each, ReLU activation

Output layer: 10 neurons (for 10 digit classes)

Model Instantiation

image

Creates and moves the model to the selected device.

Loss and Optimizer

image

CrossEntropyLoss for classification.

Adam optimizer for fast convergence.

Training Loop

image

Loops over each batch:

Moves data to device

Makes predictions

Calculates loss

Backpropagates errors

Updates model weights

Testing Loop

image

Evaluates model on test data:

Computes average loss and accuracy

pred.argmax(1) picks the most likely digit class

Training Execution

image

Trains the model for 5 epochs while monitoring performance.

Saving the Model

image

Saves the trained model's weights to disk.

Loading and Using the Model for Inference

image

Loads the saved model.

Runs inference on 10 individual images from the test set.

Compares predicted and actual digits.

About

PyTorch-MNIST-digit-recognition-model

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published