https://github.com/jackkimmins/alpha-digit
C++ Feedforward Neural Network w/ WASM Inferencing & Vue3 UI for MNIST Digit Classification
https://github.com/jackkimmins/alpha-digit
cpp emscripten feedforward-neural-network gradient-descent machine-learning mnist vue3 wasm
Last synced: about 2 months ago
JSON representation
C++ Feedforward Neural Network w/ WASM Inferencing & Vue3 UI for MNIST Digit Classification
- Host: GitHub
- URL: https://github.com/jackkimmins/alpha-digit
- Owner: jackkimmins
- Created: 2024-11-09T19:45:22.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-22T15:38:21.000Z (over 1 year ago)
- Last Synced: 2025-03-17T04:15:10.905Z (over 1 year ago)
- Topics: cpp, emscripten, feedforward-neural-network, gradient-descent, machine-learning, mnist, vue3, wasm
- Language: JavaScript
- Homepage: https://alpha-digit.appserver.uk
- Size: 6.12 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README

# Alpha-Digit
This project implements a fully connected [Feedforward Neural Network (FNN)](https://en.wikipedia.org/wiki/Feedforward_neural_network) from scratch in C++ for digit classification on the [MNIST dataset](https://en.wikipedia.org/wiki/MNIST_database) - this is a continuation from my [previous attempt](https://github.com/jackkimmins/SimpleNN) at this. The network is optimised for web deployment through [WebAssembly (WASM)](https://webassembly.org/), allowing it to perform inference in the browser.
main_train.cpp:
```cpp
#include "Database.h"
#include "NeuralNetwork.h"
#include "Evaluator.h"
#include
#include
const unsigned int seed = 42;
int main()
{
std::string dataset_path = "datasets/mnist_test.csv";
std::string model_save_path = "models/best_model.dat";
std::filesystem::create_directories("models");
// Load and split dataset into train, validation, and test sets
Database db(dataset_path, seed);
db.split_data(0.8);
// -=- Network Architecture -=- //
// Input: 784 (28x28 pixels)
// Hidden Layers: 128, 64
// Output: 10 (Digits 0-9)
int input_size = 784;
std::vector hidden_layers = { 128, 64 };
int output_size = 10;
// -=- Network Architecture -=- //
// Init Neural Network
NeuralNetwork nn(input_size, hidden_layers, output_size, seed);
// Training Hyperparameters
int epochs = 50;
int batch_size = 32;
double initial_learning_rate = 0.001;
double decay_rate = 0.9;
int decay_steps = 10;
bool early_stopping = true;
int patience = 5;
// Train the neural network
nn.train(db.get_train_data(),
db.get_validation_data(),
epochs,
batch_size,
initial_learning_rate,
decay_rate,
decay_steps,
early_stopping,
patience);
// Save the trained model
nn.save_model(model_save_path);
std::cout << "Model saved to " << model_save_path << std::endl;
// Evaluate on test data after training
Evaluator evaluator(nn, db.get_test_data());
evaluator.evaluate();
return 0;
}
```
## Project Details
- Feedforward Neural Network (FNN)
- Mini-Batch Gradient Descent w/ Adam Optimiser
- Early Stopping and Learning Rate Scheduling
- Multithreaded Training
- WebAssembly Module for Inferencing
- Vue3 UI
- High Classification Accuracy of **98.19%** *(on 7,000 image validation slice of the MNIST dataset)*
## Requirements
- C++20 or Later
- g++
- Emscripten (for WASM compilation)
## Demo
The demo site can be found at the following link:
[https://alpha-digit.appserver.uk/](https://alpha-digit.appserver.uk/)
## Mentions and Thanks
Special thanks to the following resources and individuals whose work greatly inspired and supported this project:
- [3Blue1Brown](https://www.youtube.com/@3blue1brown) - For the amazing video demonstrations and intuitive explanations of neural networks, can't recommend his videos enough! 😇
- Research Paper by Diederik P. Kingma and Jimmy Ba: Adam: A Method for Stochastic Optimization ([arXiv:1412.6980](https://arxiv.org/abs/1412.6980)).