Topic 08 · Neural Networks & Backpropagation

Train a Neural Network Live

A 2-layer MLP trains from scratch in your browser using pure JavaScript — no library. Watch weights update, loss drop, and the decision boundary form.

Dataset

Architecture

Final Loss
Accuracy
0
Epoch
Time (ms)
Decision Boundary
Loss Curve

How backpropagation works

The network makes a prediction → we compute loss → we compute the gradient of loss with respect to each weight using the chain rule → we update weights in the direction that reduces loss.

// Forward pass z1 = X @ W1 + b1 ; a1 = relu(z1) z2 = a1 @ W2 + b2 ; a2 = sigmoid(z2) loss = -mean(y*log(a2) + (1-y)*log(1-a2)) // Backward pass (chain rule) dL/dW2 = a1.T @ dL/da2 dL/dW1 = X.T @ (dL/da2 @ W2.T * relu'(z1)) // Update weights (gradient descent) W1 -= lr * dL/dW1 ; W2 -= lr * dL/dW2