This project implements a Supervised Autoencoder (SAE) to classify MNIST digits. The SAE combines the benefits of unsupervised feature learning and supervised classification into a single architecture. The autoencoder is used for dimensionality reduction, while the supervised component maps encoded features to class labels.
The MNIST Dataset is used in this project. It consists of grayscale handwritten digit images (0-9), commonly used for benchmarking machine learning models.
- Training set: 60,000 images.
- Test set: 10,000 images.
- Image size: 28x28 pixels, reshaped into a flat vector of 784 dimensions.
- Labels: One-hot encoded for multi-class classification.
- Images are normalized to the range
[0, 1]. - Labels are converted to one-hot encoded vectors for compatibility with the classification loss.
- Data is batched using PyTorch's
DataLoaderfor efficient training.
The Supervised Autoencoder (SAE) consists of the following components:
-
Encoder:
- Reduces the 784-dimensional input to a low-dimensional latent space.
- Includes linear layers with non-linear activations (ReLU).
-
Decoder:
- Reconstructs the original input from the latent representation.
- Ensures the encoder learns meaningful features for reconstruction.
-
Classifier:
- A fully connected layer that maps the latent features to class probabilities.
- Uses a softmax activation for multi-class classification.
- Learning Rate: 0.001 (tunable).
- Batch Size: 32 or 128 (used for different stages of training).
- Loss Functions:
- Reconstruction Loss: Mean Squared Error (MSE).
- Classification Loss: Cross-Entropy Loss.
- Optimizer: Adam optimizer for efficient gradient descent.
- Epochs: 40.
- The training process optimizes two objectives:
- Minimize reconstruction loss for the autoencoder.
- Minimize classification loss for the supervised component.
- Loss values are tracked across epochs for both training and validation.
- The encoder extracts low-dimensional features from the test data.
- The classifier predicts the class of each digit.
Example Output:
- Encoded feature shape:
(10000, <latent_dim>), where<latent_dim>is the size of the latent space. - Predicted class probabilities for test samples.
The model achieves the following results on the MNIST dataset:
- Reconstruction Loss: 1.523 mse
- Classification Accuracy: 94.23%
- Latent Feature Visualization: The latent features are normalized and visualized to observe separability between classes.
MNIST Supervised Autoencoders.ipynb:
A Jupyter Notebook containing the entire pipeline:- Data preprocessing.
- Model architecture definition.
- Training and evaluation.
- Visualization of results.
Ensure the following libraries are installed:
- Python 3.x
- PyTorch
- Torchmetrics (for accuracy computation).
- NumPy
- Matplotlib
- Torchvision
Install dependencies with:
pip install torch torchvision torchmetrics numpy matplotlib - Clone this repository:
git clone https://github.com/yourusername/mnist-supervised-autoencoders.git
- Navigate to the project directory and open the Jupyter Notebook:
cd mnist-supervised-autoencoders jupyter notebook - Follow the steps in the notebook to train the model and evaluate its performance.
- Experiment with deeper architectures or different loss functions.
- Apply the supervised autoencoder approach to other datasets (e.g., CIFAR-10).
- Use techniques like transfer learning or pretraining for performance improvement.
- Visualize encoded features using t-SNE or PCA for better interpretability.
Contributions are welcome! Submit a pull request or open an issue for suggestions or improvements.