Set weights and biases
Initialize once.
Calculate batch loss
One batch → one loss value.
Calculate the gradient
One vector.
One component per parameter.Update the parameters
Move opposite the gradient.
Learning rate sets the step size.Repeat with a new batch
Keep the updated parameters.
- BatchTake the next batch.Select the next 64 observations in the partition.
- LossBuild the batch loss.Sum the point losses, then divide by the batch size.
- GradientFind the gradient.Differentiate the batch loss with respect to the parameters.
- UpdateUpdate the parameters.Take one gradient step, then move to the next batch.
Target: Step Function
Training data: 2,048 observations, with uniformly spaced between and .
No noise.
Width of Two is Mathematically Sufficient
A shallow ReLU network with two hidden neurons can represent a ramp approximation to the step. As its slope increases, the transition narrows. The target stays fixed while the teal approximation becomes steeper; the finite-slope network remains continuous.
Stochastic gradient descent
- Set the initial parameters.
- Choose a batch and calculate its loss.
- Calculate the gradient of the batch loss.
- Update all parameters opposite the gradient, scaled by the learning rate.
- Repeat steps 2–4 with new batches.
Other settings: batch size, maximum epochs, and regularization strength. Early stopping ends training after a chosen number of epochs without enough improvement on validation data.
Initialize once. For each batch, evaluate the loss and its gradient at the current parameters. The gradient is one vector, with one partial-derivative component per parameter. Update all parameters together in the opposite direction, with the learning rate setting the step size. Then repeat with a new batch. The gradient changes as both the batch and the current parameters change.
Early stopping uses held-out validation data. The validation fraction, minimum improvement, and patience control when training ends. The experiment shown later instead uses a fixed 300-epoch run.
Start with two ReLU building blocks.
Combine two ReLUs to approximate the step. Both begin with a hinge at zero.
Steepen the first neuron.
Raise from 1 to 2. The positive branch steepens; zero bias keeps the hinge at zero.
Steepen and shift the second neuron.
Raise to 2, then lower to . The hinge moves to ; the positive slope stays at 2.
The hidden weight and bias act inside the ReLU.
Flip the second output contribution.
Lower from 1 to . Its contribution shrinks, then flips below zero; the hidden activation remains nonnegative.
Rust curves: dashed · solid
Add the two contributions.
Add the two weighted ReLU contributions. Beyond , their slopes cancel and the output stays at 1. The dashed blue and rust curves show the two contributions; teal shows their complete sum and dashed gray shows the target step.
Narrow the ramp with two neurons.
Call the shared input slope . First replace both coefficients with while keeping and the fitted ramp unchanged. Then increase from to : the second hinge moves to and the ramp narrows. The output weights remain and , the output bias remains zero, and the width stays at two neurons. These weights are still chosen by hand.
Now fit the network to finite data.
Now fit a network to 2,048 finite, noiseless observations from the same step-function data-generating process. The inputs are evenly spaced between and , and each outcome is . The plot shows 32 representative observations from this actual training set. The hand-chosen approximation fades away; it is not a learned fit. Stochastic gradient descent (SGD) will learn the weights and biases from these observations. No training update has occurred yet.
Batches form a partition of the data.
Shuffle 2,048 observations into 32 batches of 64. Each observation belongs to exactly one batch.
Batches cover the data without overlap. Using all 32 once completes an epoch.
Start from random weights.
Use the first batch to calculate this network’s first learning step.
: hidden weights and biases · : output weights and bias
Take the first batch.
Its 64 observations produce one update of all seven parameters.
Rust: selected batch · gray: other observations
Calculate one point’s loss.
For one observation, the prediction error is : prediction minus target. Square this error, then multiply by one-half.
Keep and fixed while calculating loss.
Add the losses across the batch.
Sum the 64 half-squared errors and divide by 64: the batch loss is half-MSE.
The 64 prediction errors give one loss value, depending on all seven parameters.
64 prediction errors. Seven parameter directions.
Each component combines the batch’s prediction errors to measure how loss changes with one parameter: four for , three for . The expanded network formula shows all seven trainable parameters.
shifts every prediction equally, so its gradient component is the mean prediction error: . Parameters remain fixed.
Update all seven parameters together.
Use the gradient just calculated for the first batch, with all seven components evaluated at the same current model.
Four values change; three have zero gradient components. Batch half-MSE: .
Next batch. New loss. New gradient.
Take the next 64 observations. Use the learned and to calculate a new loss and gradient, then update all parameters.
Both parameter blocks use the gradient evaluated at the same current model.
Finish the first 32 batches.
Each batch gives a loss, a gradient, and an update. After batch 32, every observation has been used once.
Advance the epoch counter after all 32 updates.
Begin epoch two.
Reshuffle the same observations into new batches. Keep the learned weights and update again.
Repeat across 300 epochs.
This run settles into a broad transition. Two neurons can represent a sharp ramp, but SGD does not find one here.
Accelerated epochs · ordinary full-data MSE
Can a small nudge help?
Hold six fixed; nudge one, then reset. Tiny loss gains leave a broad fit.
Full-data MSE evaluates the fit; SGD updates use batches of 64. These trials follow training.
Start again with twelve neurons.
One hidden layer, twelve neurons: 24 hidden parameters in and 13 output parameters in .
Same data, batch order, and learning rates.
Use the same 32 batches.
Still 64 observations and 64 prediction errors. Now 37 gradient components: 24 for , 13 for .
Update all 37 together, using the same current model.
Train the wider network.
This run learns a narrower transition. Labels are exact; changing batches cause the variation between updates.
Same 9,600 updates; more computation per update.
Same data. Same updates. Different fits.
Twelve neurons reach lower loss here. Extra width helps this run, but two neurons can already represent a sharp ramp.
Blue solid: width 2 · teal long dashes: width 12 · gray: target step
