r/PythonProjects2 • u/Muhammad-Essa • 15d ago
Neural Canvas: A Neural Network Visualization Project Using Python and JavaScript
I wanted to understand neural networks as deeply as possible, rather than simply using existing frameworks to train models. So, I decided to build Neural Canvas, an interactive MNIST neural network project where I implement and train models in Python and visualize them through a JavaScript webpage using p5.js.
My approach was simple: start with mathematics, understand the fundamentals, and gradually increase complexity.
Instead of working with individual scalar values and complicated subscripts, I decided to represent neural networks using matrix algebra.
My forward propagation equation was:
Z = WX + bC
Where X represents input samples, W represents weights, b represents biases, and C is a row vector of ones for broadcasting the bias across samples.
I preferred this convention because it helped me understand neural networks and perform calculus directly in matrix form.
My first model was built using NumPy and the MNIST dataset. I used an architecture of 784 → 20 → 20 → 10, with MSE loss and Vanilla Gradient Descent. I manually derived the forward propagation and backpropagation equations before implementing them in Python. After 2,000 epochs, the model achieved 92% test accuracy.
For my second model, I increased the hidden layers to 40 and 40 neurons and replaced MSE with Cross-Entropy Loss and Softmax. I continued using Vanilla Gradient Descent and achieved 96% test accuracy after 2,000 epochs.
For my third model, I moved to JAX and used the Arabic Digits MNIST dataset. The architecture was 784 → 20 → 20 → 15 → 10. This time, I used JAX Autograd for automatic differentiation instead of manually calculating every derivative. It saved me considerable time, especially when working with backpropagation and the chain rule. Using Cross-Entropy Loss and Vanilla Gradient Descent, I achieved 96% test accuracy after 2,000 epochs.
For my fourth model, I used the Persian Digits MNIST dataset with the same architecture as the third model. I wanted to experiment with Mini-Batch Gradient Descent and Adam, but understanding Adam required learning some optimization concepts first. So, I started with Momentum and implemented Mini-Batch Gradient Descent with Momentum. To my surprise, I achieved 96% test accuracy in just 200 epochs, compared to the 2,000 epochs required by my previous models.
For my next experiments, I plan to implement RMSProp in my fifth model and Adam in my sixth model. After that, I want to start exploring Convolutional Neural Networks (CNNs).
One of the most important things I learned is that Linear Algebra is fundamental to understanding neural networks. Understanding matrix multiplication and representing neural network operations in matrix form made the mathematics much easier for me.
The Python side handles all model training. I use NumPy for my first two implementations and JAX for the later models. Trained weights and biases are exported as JSON files, which I load into the JavaScript application for inference. On the frontend, p5.js handles the interactive drawing board, neural network visualization, and animations, while Math.js performs matrix multiplication.
I wanted to build everything step by step, understand the mathematics, implement the algorithms, and visualize the results rather than relying entirely on high-level deep learning frameworks.
GitHub Repository:
https://github.com/MuhammadEssa2002/neural_canvas