Built from scratch · Python · NumPy · Summer 2026

NeuralNet

A feedforward neural network built from scratch using only Python and NumPy, no PyTorch, no TensorFlow, no Keras. Forward pass, backpropagation, and mini-batch gradient descent, all hand-derived and hand-coded. Trained on MNIST to 98.0% test accuracy. The digit classifier below runs the network's real trained weights, ported to JavaScript, doing genuine inference on whatever you draw.

NumPy Backpropagation MNIST 98.0% Accuracy Flask

Draw a digit, 0 through 9. This isn't a simulation, the actual weights from mnist_weights.npz were exported and are running the same forward pass shown in the code tab, just in JavaScript instead of NumPy.

784 → 256 → 128 → 10 · He init · Adam optimizer
– Draw a digit, 0 through 9, on the canvas.

A neural network is a function with millions of parameters (weights). Training finds parameter values that minimize error on the training data. This one has none of that magic hidden behind a framework, every piece below is code I wrote.

784
Input (28×28 pixels)
→
256
Dense + ReLU
→
128
Dense + ReLU
→
10
Softmax (digit probabilities)
→
Forward pass
Each layer computes Z = X·W + b, then applies an activation. Input flows through every layer in sequence, producing the final prediction.
✕
Loss: Cross-Entropy
Measures how wrong the prediction is: -sum(true_label · log(predicted_prob)). Combined with softmax, its gradient simplifies beautifully to predictions - labels.
←
Backpropagation
The chain rule, applied layer by layer from the output backwards, tells us how much each weight contributed to the error, so we know which direction to nudge it.
⚙
He Initialization
Weights start as N(0, √(2/n)) instead of zero. If every weight started at zero, every neuron in a layer would compute the same output and the network could never differentiate between them.

The actual Python source: forward pass, backward pass, loss, and how the Flask app prepares a drawing. The page above goes one step further. It redraws your strokes the way MNIST digits were made (fit in 20x20 pixels, about 2 pixels thick, centred by mass), which makes its guesses much better.

What I learned

The softmax + cross-entropy gradient simplification is beautiful.

I derived it by hand: the gradient of cross-entropy(softmax(z)) with respect to z simplifies to just predictions - labels. Seeing pages of chain-rule algebra collapse into one clean line is why this combination is used everywhere in classification.

Weight initialization isn't a minor detail.

If every weight starts at zero, every neuron in a layer computes the same output and receives the same gradient forever, they can never specialize. He initialization keeps gradient magnitudes stable specifically for ReLU layers.

Mini-batches are a genuine trade-off, not an arbitrary default.

Batch size 1 gives noisy gradient estimates; the full dataset at once is accurate but slow. 32-256 samples turned out to be the sweet spot: fast, and with enough gradient variance to escape local minima.