Skip to content

This repository contains a PyTorch implementation of a Multi-Layer Perceptron (MLP) for handwritten digit classification using the MNIST dataset. The project includes data loading, preprocessing, training, validation, and evaluation.

Notifications You must be signed in to change notification settings

Ali-Morsal/MNIST-Digit-Classification-with-MLP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

MNIST Digit Classification with MLP

This repository contains a PyTorch implementation of a Multi-Layer Perceptron (MLP) for handwritten digit classification using the MNIST dataset. The project includes data loading, preprocessing, augmentation, training, validation, and evaluation.


Table of Contents


Project Overview

This project demonstrates how to build a fully connected neural network (MLP) for image classification. The network is trained on the MNIST dataset, which contains 70,000 grayscale images of handwritten digits (28x28 pixels) split into training and test sets.


Dataset

The MNIST dataset is used in IDX format. The dataset includes:

  • Training images: 60,000
  • Test images: 10,000
  • Classes: 10 (digits 0–9)

Custom data loading functions are included to read the .idx files and convert them to PyTorch tensors.


Features

  • Normalization of input images
  • Training/validation split (90%/10%)
  • Fully connected MLP with dropout for regularization
  • Dynamic learning rate scheduling using OneCycleLR
  • Logging of training, validation, and test losses per epoch

Model Architecture

  • Input Layer: 784 neurons (28x28 flattened images)
  • Hidden Layer 1: 512 neurons + Dropout(0.3)
  • Hidden Layer 2: 256 neurons + Dropout(0.2)
  • Output Layer: 10 neurons (softmax via CrossEntropyLoss)

Training

  • Optimizer: Adam
  • Learning Rate: 0.001 (managed by OneCycleLR with max_lr=0.005)
  • Loss Function: CrossEntropyLoss
  • Epochs: 80
  • Batch Size: 256

Training includes automatic validation and testing at each epoch. Augmented data is included in the training set to improve generalization.


Results

  • Final Test Accuracy: >98%

To achieve higher accuracy (>99%), a CNN is recommended.


About

This repository contains a PyTorch implementation of a Multi-Layer Perceptron (MLP) for handwritten digit classification using the MNIST dataset. The project includes data loading, preprocessing, training, validation, and evaluation.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published