This repository contains the complete solution for the M1 Coursework for the MPhil in Data Intensive Science program. The project explores the implementation, hyperparameter tuning, and analysis of Fully Connected Neural Networks (FCNNs) applied to image classification and dimensionality reduction tasks.
The coursework is split into two main themes:
- Handwritten Digit Classification: Building an optimized FCNN, ensuring deterministic reproducibility, and comparing performance against the standard MNIST dataset.
- Dimensionality Reduction with Triplet Loss: Implementing a custom triplet loss function to train a network that maps the CIFAR-10 dataset into a restricted 2D latent space, followed by a comparative analysis against standard t-SNE embeddings.
solution.ipynb: The main Jupyter Notebook containing all the code, training loops, visualizations, and written discussions for Tasks 1 through 6.m1-coursework-2025.pdf: The original coursework instruction document.requirements.txt: The Python dependencies required to run the notebook and ensure complete reproducibility.best_model_task2.keras: The saved weights of the best-performing neural network found during hyperparameter tuning in Task 2.best_model_cifar_task5.keras: The saved weights of the CIFAR-10 latent space embedding model trained in Task 5.figs/: A directory containing all the plots and visualizations generated by the notebook (e.g.,cifar_embeddings_vs_tsne.png,training_progress.png,mnist_misclassified.png).
- Task 1 (Hyperparameter Tuning): Utilizes
Optunato search for the optimal network depth, hidden layer size, dropout rate, activation functions, and learning rate for an FCNN classifying a custom handwritten digit dataset. - Task 2 (Reproducibility): Demonstrates strict computational reproducibility by fixing random seeds and enabling TensorFlow operation determinism (
tf.config.experimental.enable_op_determinism()). It verifies that identical inference results are produced both before and after training across multiple runs. - Task 3 (Analysis & MNIST Comparison): Evaluates the trained network on the standard MNIST dataset to check for generalization and domain shift, analyzing training vs. validation accuracy and examining misclassified examples.
- Task 4 (Custom Loss Function): Implements an analytic triplet loss function designed to penalize the distance between embeddings of different classes while minimizing the distance between embeddings of the same class.
- Task 5 (Latent Space Mapping): Trains an FCNN using the custom triplet loss to map the complex CIFAR-10 dataset into a 2D latent space, utilizing a soft margin constraint.
- Task 6 (Visualization): Visualizes the learned 2D embeddings and directly compares the clustering quality against a standard t-SNE transformation of the original CIFAR-10 data using
scikit-learn.
- Install Dependencies: Ensure you have Python installed, then install the required packages using:
pip install -r requirements.txt- Run the Notebook:
Open the
solution.ipynbfile in Jupyter Notebook or JupyterLab.
- Run the
Initializationblock first to load all required libraries and set up the global environment. - You can run the entire notebook end-to-end to reproduce all outputs, or execute specific task sections individually.
- All generated plots will be automatically saved to the
figs/folder.
- GitHub Copilot was utilized to assist with writing certain portions of the codebase, primarily for drafting docstrings and matplotlib plotting boilerplate.