Training a Neural Network in Pure HTML and CSS
- 1 · Input
- 2 · Prediction
- 3 · Loss
- 4 · Gradients
- 5 · Write weights
- 6 · Commit
1 · The input: current training example
Each image is an 8×8 grid: an inked pixel is 1 and a blank pixel is 0. The page cycles through 24 fixed X and O examples. The truth label is the correct answer used to check the network's guess; one full pass through these images is an epoch.
truth: OX · image # of 24
2 · The guess: forward pass
Each 3×3 kernel slides over the image, multiplying its nine weights by the pixels underneath and adding a bias. The same weights are reused at every position, producing one 6×6 feature map per kernel. ReLU sets negative results to zero; max pooling keeps the largest value in each 2×2 block, leaving two 3×3 maps.
prediction: OX p(O) = %
The dense layer combines the 18 pooled values into an X score and an O score. Softmax converts them to probabilities that add up to 100%; p(O) is the probability of O. Blue in the bar means O, orange means X. The map outlines mark the cells selected by max pooling.
3 · The score: loss (how wrong was the guess?)
Cross-entropy is −ln(probability of the correct answer): a confident correct guess has low loss, while a confident wrong guess has high loss. The first readout scores the current image. The second evaluates all 24 training images with the current weights, and the curve stores that average every 6 updates.
Probability the network gave to the correct answer for this image: %
Loss on this image = −ln(that probability)
Average loss over all 24 training images, with the weights as they are right now:
Training loss over time, sampled every 6 SGD steps
stored sample being written now current value score of a 50/50 guess
The curve keeps the latest 50 samples; its dots are rounded to 0.04 and capped at 2.56. The numerical readout shows the live loss to three decimals. Individual updates can make loss rise temporarily, so watch the trend over several epochs.
4 · The blame: backprop + SGD update
Backpropagation works backward through the network to calculate a gradient for each of its 58 trainable parameters: how a small change would affect loss. SGD updates each value using old value − learning rate × gradient, then rounds to increments of 0.125. Blue cells below indicate an increase; orange cells indicate a decrease.
Output error = p(O) − truth = −
Weight change, units of 0.125 (blue = up, orange = down)
This step changes of 58 weights.
5 · Automatic step advance: the clock
Press Start once. CSS calculates the next weights and writes them into the spare bank. When every weight—and any scheduled loss sample—has reached its target, a commit timer waits 0.15 seconds, then advances the step counter. That switches banks and selects the next image. Pause freezes progress; Reset restores the starting state.
Writing: weight registers are still moving towards their targets. Step written: committing and loading the next example. Paused. Press Start; no further input is needed.
6 · Weight memory: where the numbers live
Each parameter is stored in the elapsed time of paused CSS animations. To change it, an animation runs toward its target and pauses again. A marker moves with that stored value; a view timeline reads its position back into the calculations. Bank A holds the current weights while B is written, then they swap roles, so an update reads a consistent set of weights.
Banks A and B (58 weights each). The highlighted bank holds the current weights and stays still; the other is being written. Blue: step counter. Red: commit timer. Orange tint: the 50 loss-history registers behind the chart (65 levels of 0.04).
How good is it? Live accuracy from the current weights
Every thumbnail makes its own live prediction using the current weights. Training accuracy measures the examples used for learning. The held-out test set contains glyphs never used for weight updates and checks whether learning carries over to other shapes. A red border marks a wrong answer; the bar below each image shows p(O).
training set: / 24 correct (red border = wrong)
held-out test set: / 84 correct (red border = wrong)
How it works: one complete training step
The browser repeats this sequence using HTML and CSS. The images are fixed, but the predictions, gradients, stored weights, and loss samples are computed as the experiment runs.
- Read an image.The step counter selects an 8×8 glyph and its X or O label from the training set.
- Make a prediction.The shared convolution filters, ReLU, pooling, and dense layer turn the pixels into two probabilities.
- Measure the error.The correct label tells the loss formula how much probability the network should have assigned to that answer.
- Calculate a change.Backpropagation follows the chain rule through the layers. For each shared kernel weight, it adds contributions from every position where that weight was used. SGD uses these gradients to choose the next parameter values.
- Store the next state.The spare animation bank moves to those values. Every 6 steps, a history register also stores the current average training loss for the curve.
- Commit and repeat.Once the writes finish, the CSS clock advances. The new bank becomes current, the next image is selected, and the calculation starts again.
What HTML and CSS each do
HTML provides the elements, controls, and fixed data. CSS custom properties carry numbers; arithmetic functions compute the forward pass, loss, and gradients. CSS conditions control when animations run. Animation time supplies persistent state, and view timelines feed that state back into the next calculation. Together these pieces form the automatic training loop.
Why this is a real trainable CNN
The two kernels reuse their weights across the image, and backpropagation updates those kernel weights along with the dense layer. The weights start from initial values and change during training. The output is computed from the current weights, and the loss curve reads stored samples from the run.