POLS 4728 / Machine learningPrevious: shallow networks

Training
neural networks

Stochastic Gradient Descent

Scroll to begin ↓
Two round, torso-less figures steer small racing hovercraft down a hilly, faceted landscape.

Target: Step Function

yi=f(xi)={0,xi≤0,1,xi>0.\textcolor{#606060}{\textcolor{#a54f32}{y_i}=f(x_i)=\begin{cases}0,&x_i\le0,\\[5pt]1,&x_i>0.\end{cases}}

Training data: 2,048 observations, with xx uniformly spaced between −1-1 and 11.

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 f(x)f(x) stays fixed while the teal approximation becomes steeper; the finite-slope network remains continuous.

Stochastic gradient descent

  1. Set the initial parameters.
  2. Choose a batch and calculate its loss.
  3. Calculate the gradient of the batch loss.
  4. Update all parameters opposite the gradient, scaled by the learning rate.
  5. 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 gB=∇(θ,ϕ)LBg_B=\nabla_{(\theta,\phi)}L_B is one vector, with one partial-derivative component per parameter. Update all parameters together in the opposite direction, with the learning rate α\alpha 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.

h1(x)=ReLU⁡(x),h2(x)=ReLU⁡(x)\textcolor{#234e70}{h_1(x)=\operatorname{ReLU}(x),}\qquad\textcolor{#a54f32}{ h_2(x)=\operatorname{ReLU}(x)}

Combine two ReLUs to approximate the step. Both begin with a hinge at zero.

Steepen the first neuron.

h1(x)=ReLU⁡(θ10+θ11x),θ11:1→2, θ10=0\textcolor{#234e70}{h_1(x)=\operatorname{ReLU}(\theta_{10}+\theta_{11}x),\qquad \theta_{11}:1\to2,\ \theta_{10}=0}

Raise θ11\theta_{11} from 1 to 2. The positive branch steepens; zero bias keeps the hinge at zero.

Steepen and shift the second neuron.

h2(x)=ReLU⁡(θ20+θ21x) ⟶ ReLU⁡(2x−1)\textcolor{#a54f32}{h_2(x)=\operatorname{ReLU}(\theta_{20}+\theta_{21}x)\ \longrightarrow\ \operatorname{ReLU}(2x-1)}

Raise θ21\theta_{21} to 2, then lower θ20\theta_{20} to −1-1. The hinge moves to 1/21/2; the positive slope stays at 2.

The hidden weight and bias act inside the ReLU.

Flip the second output contribution.

ϕ1h1(x)=h1(x),ϕ2h2(x),ϕ2:1→−1\textcolor{#234e70}{\phi_1h_1(x)=h_1(x),}\qquad\textcolor{#a54f32}{ \phi_2h_2(x),\quad \phi_2:1\to-1}

Lower ϕ2\phi_2 from 1 to −1-1. Its contribution shrinks, then flips below zero; the hidden activation remains nonnegative.

Rust curves: dashed h2h_2 · solid ϕ2h2\phi_2h_2

Add the two contributions.

f^(x)=h1(x)−h2(x)=ReLU⁡(2x)−ReLU⁡(2x−1)\textcolor{#007f73}{\hat f(x)=h_1(x)-h_2(x)=\operatorname{ReLU}(2x)-\operatorname{ReLU}(2x-1)}

Add the two weighted ReLU contributions. Beyond 1/21/2, 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.

f^(x)=ReLU⁡(kx)−ReLU⁡(kx−1)\textcolor{#007f73}{\hat f(x)=\operatorname{ReLU}(kx)-\operatorname{ReLU}(kx-1)}

Call the shared input slope kk. First replace both coefficients 22 with kk while keeping k=2k=2 and the fitted ramp unchanged. Then increase kk from 22 to 3232: the second hinge moves to 1/k1/k and the ramp narrows. The output weights remain +1+1 and −1-1, 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.

D={(xi,yi)}i=12048,yi=f(xi)\textcolor{#234e70}{\mathcal D=\{(x_i,y_i)\}_{i=1}^{2048},\qquad y_i=\textcolor{#606060}{f(x_i)}}

Now fit a network to 2,048 finite, noiseless observations from the same step-function data-generating process. The inputs are evenly spaced between −1-1 and 11, and each outcome is yi=f(xi)y_i=f(x_i). 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.

D=B1∪⋯∪B32,Bj∩Bk=∅ (j≠k)\mathcal D=\textcolor{#a54f32}{B_1}\cup\cdots\cup B_{32},\qquad B_j\cap B_k=\varnothing\ (j\ne k)

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.

f^(x;θ,ϕ)=ϕ0+∑j=12ϕjReLU⁡(θj0+θj1x)\textcolor{#234e70}{\hat f(x;\theta,\phi)=\phi_0+\sum_{j=1}^{2}\phi_j\operatorname{ReLU}(\theta_{j0}+\theta_{j1}x)}

Use the first batch to calculate this network’s first learning step.

θ\theta: hidden weights and biases · ϕ\phi: output weights and bias

Take the first batch.

B1={first 64 shuffled observations}\textcolor{#a54f32}{B_1=\{\text{first 64 shuffled observations}\}}

Its 64 observations produce one update of all seven parameters.

Rust: selected batch · gray: other observations

Calculate one point’s loss.

ℓi(θ,ϕ)=12(f^(xi;θ,ϕ)−yi)2,i∈B1\textcolor{#a54f32}{\ell_i(\theta,\phi)}=\frac12\bigl(\textcolor{#234e70}{\hat f(x_i;\theta,\phi)}-\textcolor{#a54f32}{y_i}\bigr)^2,\qquad i\in \textcolor{#a54f32}{B_1}

For one observation, the prediction error is ei=f^(xi;θ,ϕ)−yie_i=\hat f(x_i;\theta,\phi)-y_i: prediction minus target. Square this error, then multiply by one-half.

Keep θ\theta and ϕ\phi fixed while calculating loss.

Add the losses across the batch.

SB1(θ,ϕ)=∑i∈B1ℓi(θ,ϕ),LB1(θ,ϕ)=SB1(θ,ϕ)64\textcolor{#a54f32}{S_{B_1}(\theta,\phi)}=\sum_{i\in B_1}\textcolor{#a54f32}{\ell_i(\theta,\phi)},\qquad \textcolor{#234e70}{L_{B_1}(\theta,\phi)}=\frac{\textcolor{#a54f32}{S_{B_1}(\theta,\phi)}}{64}

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.

f^(x;θ,ϕ)=ϕ0+ϕ1ReLU⁡(θ10+θ11x)+ϕ2ReLU⁡(θ20+θ21x)g1=∇(θ,ϕ)LB1(θ,ϕ)\textcolor{#234e70}{\begin{aligned}\hat f(x;\theta,\phi)&=\textcolor{#007f73}{\phi_0}+\phi_1\operatorname{ReLU}(\theta_{10}+\theta_{11}x)\\&\quad+\phi_2\operatorname{ReLU}(\theta_{20}+\theta_{21}x)\end{aligned}\qquad g_1=\nabla_{(\theta,\phi)}L_{B_1}(\theta,\phi)}

Each component combines the batch’s prediction errors to measure how loss changes with one parameter: four for θ\theta, three for ϕ\phi. The expanded network formula shows all seven trainable parameters.

ϕ0\phi_0 shifts every prediction equally, so its gradient component is the mean prediction error: −0.1681-0.1681. Parameters remain fixed.

Update all seven parameters together.

θ←θ−α0∇θLB1(θ,ϕ),ϕ←ϕ−α0∇ϕLB1(θ,ϕ)\textcolor{#234e70}{\theta\leftarrow\theta-\alpha_0\nabla_\theta L_{B_1}(\theta,\phi),\qquad\phi\leftarrow\phi-\alpha_0\nabla_\phi L_{B_1}(\theta,\phi)}

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: 0.2129→0.20450.2129\to0.2045.

Next batch. New loss. New gradient.

θ←θ−α1∇θLB2(θ,ϕ),ϕ←ϕ−α1∇ϕLB2(θ,ϕ)\textcolor{#234e70}{\theta\leftarrow\theta-\alpha_1\nabla_\theta L_{B_2}(\theta,\phi),\qquad\phi\leftarrow\phi-\alpha_1\nabla_\phi L_{B_2}(\theta,\phi)}

Take the next 64 observations. Use the learned θ\theta and ϕ\phi 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.

32 batches×64 observations=2,04832\text{ batches}\times64\text{ observations}=2{,}048

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.

1 epoch=1 complete pass through the data1\text{ epoch}=1\text{ complete pass through the data}

Reshuffle the same observations into new batches. Keep the learned weights and update again.

Repeat across 300 epochs.

m=2,αt=0.051+t/6000\textcolor{#234e70}{m=2},\qquad\alpha_t=\frac{0.05}{1+t/6000}

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?

f^(x)=ϕ0+ϕ1ReLU⁡(θ10+θ11x)+ϕ2ReLU⁡(θ20+θ21x)\textcolor{#234e70}{\hat f(x)=\phi_0+\phi_1\operatorname{ReLU}(\theta_{10}+\theta_{11}x)+\phi_2\operatorname{ReLU}(\theta_{20}+\theta_{21}x)}

Hold six fixed; nudge one, then reset. Tiny loss gains leave a broad fit.

1^{1} Full-data MSE evaluates the fit; SGD updates use batches of 64. These trials follow training.

Start again with twelve neurons.

f^(x;θ,ϕ)=ϕ0+∑j=112ϕjReLU⁡(θj0+θj1x)\textcolor{#007f73}{\hat f(x;\theta,\phi)=\phi_0+\sum_{j=1}^{12}\phi_j\operatorname{ReLU}(\theta_{j0}+\theta_{j1}x)}

One hidden layer, twelve neurons: 24 hidden parameters in θ\theta and 13 output parameters in ϕ\phi.

Same data, batch order, and learning rates.

Use the same 32 batches.

θ←θ−αt∇θLB(θ,ϕ),ϕ←ϕ−αt∇ϕLB(θ,ϕ)\textcolor{#007f73}{\theta\leftarrow\theta-\alpha_t\nabla_\theta L_B(\theta,\phi),\qquad\phi\leftarrow\phi-\alpha_t\nabla_\phi L_B(\theta,\phi)}

Still 64 observations and 64 prediction errors. Now 37 gradient components: 24 for θ\theta, 13 for ϕ\phi.

Update all 37 together, using the same current model.

Train the wider network.

m=12,300 epochs=9,600 updates\textcolor{#007f73}{m=12},\qquad300\text{ epochs}=9{,}600\text{ updates}

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.

m=2versusm=12\textcolor{#234e70}{m=2}\qquad\text{versus}\qquad \textcolor{#007f73}{m=12}

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

Across random initializations

Different starts. Different fits.

Plain SGD · 300 epochs · same data and batch order

FinalInitializationf(x)f(x) target

Showing initialization expands every plot to the same vertical range. Turning it off restores the shared final-fit axes. The MSE labels always describe the final fits.

Width 2

Start 1MSE=0.2500\mathrm{MSE}=0.2500
Width 2, start 1. Final training mean squared error 0.2500. Solid final function. Dashed gray target step. 01 −101
xx
Width 2, start 1. Final training mean squared error 0.2500. Faint dotted initial function and solid final function on shared expanded axes. Dashed gray target step. −4013 −101
xx
Start 2MSE=0.0556\mathrm{MSE}=0.0556
Width 2, start 2. Final training mean squared error 0.0556. Solid final function. Dashed gray target step. 01 −101
xx
Width 2, start 2. Final training mean squared error 0.0556. Faint dotted initial function and solid final function on shared expanded axes. Dashed gray target step. −4013 −101
xx
Start 3MSE=0.0625\mathrm{MSE}=0.0625
Width 2, start 3. Final training mean squared error 0.0625. Solid final function. Dashed gray target step. 01 −101
xx
Width 2, start 3. Final training mean squared error 0.0625. Faint dotted initial function and solid final function on shared expanded axes. Dashed gray target step. −4013 −101
xx
Start 4MSE=0.0556\mathrm{MSE}=0.0556
Width 2, start 4. Final training mean squared error 0.0556. Solid final function. Dashed gray target step. 01 −101
xx
Width 2, start 4. Final training mean squared error 0.0556. Faint dotted initial function and solid final function on shared expanded axes. Dashed gray target step. −4013 −101
xx
Start 5MSE=0.0556\mathrm{MSE}=0.0556
Width 2, start 5. Final training mean squared error 0.0556. Solid final function. Dashed gray target step. 01 −101
xx
Width 2, start 5. Final training mean squared error 0.0556. Faint dotted initial function and solid final function on shared expanded axes. Dashed gray target step. −4013 −101
xx
Start 6MSE=0.0556\mathrm{MSE}=0.0556
Width 2, start 6. Final training mean squared error 0.0556. Solid final function. Dashed gray target step. 01 −101
xx
Width 2, start 6. Final training mean squared error 0.0556. Faint dotted initial function and solid final function on shared expanded axes. Dashed gray target step. −4013 −101
xx
Start 7MSE=0.0556\mathrm{MSE}=0.0556
Width 2, start 7. Final training mean squared error 0.0556. Solid final function. Dashed gray target step. 01 −101
xx
Width 2, start 7. Final training mean squared error 0.0556. Faint dotted initial function and solid final function on shared expanded axes. Dashed gray target step. −4013 −101
xx
Start 8MSE=0.0124\mathrm{MSE}=0.0124
Width 2, start 8. Final training mean squared error 0.0124. Solid final function. Dashed gray target step. 01 −101
xx
Width 2, start 8. Final training mean squared error 0.0124. Faint dotted initial function and solid final function on shared expanded axes. Dashed gray target step. −4013 −101
xx

Width 12

Start 1MSE=0.0149\mathrm{MSE}=0.0149
Width 12, start 1. Final training mean squared error 0.0149. Solid final function. Dashed gray target step. 01 −101
xx
Width 12, start 1. Final training mean squared error 0.0149. Faint dotted initial function and solid final function on shared expanded axes. Dashed gray target step. −4013 −101
xx
Start 2MSE=0.0166\mathrm{MSE}=0.0166
Width 12, start 2. Final training mean squared error 0.0166. Solid final function. Dashed gray target step. 01 −101
xx
Width 12, start 2. Final training mean squared error 0.0166. Faint dotted initial function and solid final function on shared expanded axes. Dashed gray target step. −4013 −101
xx
Start 3MSE=0.0151\mathrm{MSE}=0.0151
Width 12, start 3. Final training mean squared error 0.0151. Solid final function. Dashed gray target step. 01 −101
xx
Width 12, start 3. Final training mean squared error 0.0151. Faint dotted initial function and solid final function on shared expanded axes. Dashed gray target step. −4013 −101
xx
Start 4MSE=0.0179\mathrm{MSE}=0.0179
Width 12, start 4. Final training mean squared error 0.0179. Solid final function. Dashed gray target step. 01 −101
xx
Width 12, start 4. Final training mean squared error 0.0179. Faint dotted initial function and solid final function on shared expanded axes. Dashed gray target step. −4013 −101
xx
Start 5MSE=0.0132\mathrm{MSE}=0.0132
Width 12, start 5. Final training mean squared error 0.0132. Solid final function. Dashed gray target step. 01 −101
xx
Width 12, start 5. Final training mean squared error 0.0132. Faint dotted initial function and solid final function on shared expanded axes. Dashed gray target step. −4013 −101
xx
Start 6MSE=0.0188\mathrm{MSE}=0.0188
Width 12, start 6. Final training mean squared error 0.0188. Solid final function. Dashed gray target step. 01 −101
xx
Width 12, start 6. Final training mean squared error 0.0188. Faint dotted initial function and solid final function on shared expanded axes. Dashed gray target step. −4013 −101
xx
Start 7MSE=0.0199\mathrm{MSE}=0.0199
Width 12, start 7. Final training mean squared error 0.0199. Solid final function. Dashed gray target step. 01 −101
xx
Width 12, start 7. Final training mean squared error 0.0199. Faint dotted initial function and solid final function on shared expanded axes. Dashed gray target step. −4013 −101
xx
Start 8MSE=0.0348\mathrm{MSE}=0.0348
Width 12, start 8. Final training mean squared error 0.0348. Solid final function. Dashed gray target step. 01 −101
xx
Width 12, start 8. Final training mean squared error 0.0348. Faint dotted initial function and solid final function on shared expanded axes. Dashed gray target step. −4013 −101
xx

All 8 starts shown for each width.

References

Prince, Simon J. D. (2023). Understanding Deep Learning. MIT Press.
Chapter 3: Shallow neural networks. Chapter 6: Fitting models.

Read the complete explanation without animation

Target: Step Function

yi=f(xi)={0,xi≤0,1,xi>0.\textcolor{#606060}{\textcolor{#a54f32}{y_i}=f(x_i)=\begin{cases}0,&x_i\le0,\\[5pt]1,&x_i>0.\end{cases}}

Training data: 2,048 observations, with xx uniformly spaced between −1-1 and 11.

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 f(x)f(x) stays fixed while the teal approximation becomes steeper; the finite-slope network remains continuous.

Stochastic gradient descent

  1. Set the initial parameters.
  2. Choose a batch and calculate its loss.
  3. Calculate the gradient of the batch loss.
  4. Update all parameters opposite the gradient, scaled by the learning rate.
  5. 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 gB=∇(θ,ϕ)LBg_B=\nabla_{(\theta,\phi)}L_B is one vector, with one partial-derivative component per parameter. Update all parameters together in the opposite direction, with the learning rate α\alpha 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.

h1(x)=ReLU⁡(x),h2(x)=ReLU⁡(x)\textcolor{#234e70}{h_1(x)=\operatorname{ReLU}(x),}\qquad\textcolor{#a54f32}{ h_2(x)=\operatorname{ReLU}(x)}

Combine two ReLUs to approximate the step. Both begin with a hinge at zero.

Steepen the first neuron.

h1(x)=ReLU⁡(θ10+θ11x),θ11:1→2, θ10=0\textcolor{#234e70}{h_1(x)=\operatorname{ReLU}(\theta_{10}+\theta_{11}x),\qquad \theta_{11}:1\to2,\ \theta_{10}=0}

Raise θ11\theta_{11} from 1 to 2. The positive branch steepens; zero bias keeps the hinge at zero.

Steepen and shift the second neuron.

h2(x)=ReLU⁡(θ20+θ21x) ⟶ ReLU⁡(2x−1)\textcolor{#a54f32}{h_2(x)=\operatorname{ReLU}(\theta_{20}+\theta_{21}x)\ \longrightarrow\ \operatorname{ReLU}(2x-1)}

Raise θ21\theta_{21} to 2, then lower θ20\theta_{20} to −1-1. The hinge moves to 1/21/2; the positive slope stays at 2.

The hidden weight and bias act inside the ReLU.

Flip the second output contribution.

ϕ1h1(x)=h1(x),ϕ2h2(x),ϕ2:1→−1\textcolor{#234e70}{\phi_1h_1(x)=h_1(x),}\qquad\textcolor{#a54f32}{ \phi_2h_2(x),\quad \phi_2:1\to-1}

Lower ϕ2\phi_2 from 1 to −1-1. Its contribution shrinks, then flips below zero; the hidden activation remains nonnegative.

Rust curves: dashed h2h_2 · solid ϕ2h2\phi_2h_2

Add the two contributions.

f^(x)=h1(x)−h2(x)=ReLU⁡(2x)−ReLU⁡(2x−1)\textcolor{#007f73}{\hat f(x)=h_1(x)-h_2(x)=\operatorname{ReLU}(2x)-\operatorname{ReLU}(2x-1)}

Add the two weighted ReLU contributions. Beyond 1/21/2, 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.

f^(x)=ReLU⁡(kx)−ReLU⁡(kx−1)\textcolor{#007f73}{\hat f(x)=\operatorname{ReLU}(kx)-\operatorname{ReLU}(kx-1)}

Call the shared input slope kk. First replace both coefficients 22 with kk while keeping k=2k=2 and the fitted ramp unchanged. Then increase kk from 22 to 3232: the second hinge moves to 1/k1/k and the ramp narrows. The output weights remain +1+1 and −1-1, 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.

D={(xi,yi)}i=12048,yi=f(xi)\textcolor{#234e70}{\mathcal D=\{(x_i,y_i)\}_{i=1}^{2048},\qquad y_i=\textcolor{#606060}{f(x_i)}}

Now fit a network to 2,048 finite, noiseless observations from the same step-function data-generating process. The inputs are evenly spaced between −1-1 and 11, and each outcome is yi=f(xi)y_i=f(x_i). 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.

D=B1∪⋯∪B32,Bj∩Bk=∅ (j≠k)\mathcal D=\textcolor{#a54f32}{B_1}\cup\cdots\cup B_{32},\qquad B_j\cap B_k=\varnothing\ (j\ne k)

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.

f^(x;θ,ϕ)=ϕ0+∑j=12ϕjReLU⁡(θj0+θj1x)\textcolor{#234e70}{\hat f(x;\theta,\phi)=\phi_0+\sum_{j=1}^{2}\phi_j\operatorname{ReLU}(\theta_{j0}+\theta_{j1}x)}

Use the first batch to calculate this network’s first learning step.

θ\theta: hidden weights and biases · ϕ\phi: output weights and bias

Take the first batch.

B1={first 64 shuffled observations}\textcolor{#a54f32}{B_1=\{\text{first 64 shuffled observations}\}}

Its 64 observations produce one update of all seven parameters.

Rust: selected batch · gray: other observations

Calculate one point’s loss.

ℓi(θ,ϕ)=12(f^(xi;θ,ϕ)−yi)2,i∈B1\textcolor{#a54f32}{\ell_i(\theta,\phi)}=\frac12\bigl(\textcolor{#234e70}{\hat f(x_i;\theta,\phi)}-\textcolor{#a54f32}{y_i}\bigr)^2,\qquad i\in \textcolor{#a54f32}{B_1}

For one observation, the prediction error is ei=f^(xi;θ,ϕ)−yie_i=\hat f(x_i;\theta,\phi)-y_i: prediction minus target. Square this error, then multiply by one-half.

Keep θ\theta and ϕ\phi fixed while calculating loss.

Add the losses across the batch.

SB1(θ,ϕ)=∑i∈B1ℓi(θ,ϕ),LB1(θ,ϕ)=SB1(θ,ϕ)64\textcolor{#a54f32}{S_{B_1}(\theta,\phi)}=\sum_{i\in B_1}\textcolor{#a54f32}{\ell_i(\theta,\phi)},\qquad \textcolor{#234e70}{L_{B_1}(\theta,\phi)}=\frac{\textcolor{#a54f32}{S_{B_1}(\theta,\phi)}}{64}

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.

f^(x;θ,ϕ)=ϕ0+ϕ1ReLU⁡(θ10+θ11x)+ϕ2ReLU⁡(θ20+θ21x)g1=∇(θ,ϕ)LB1(θ,ϕ)\textcolor{#234e70}{\begin{aligned}\hat f(x;\theta,\phi)&=\textcolor{#007f73}{\phi_0}+\phi_1\operatorname{ReLU}(\theta_{10}+\theta_{11}x)\\&\quad+\phi_2\operatorname{ReLU}(\theta_{20}+\theta_{21}x)\end{aligned}\qquad g_1=\nabla_{(\theta,\phi)}L_{B_1}(\theta,\phi)}

Each component combines the batch’s prediction errors to measure how loss changes with one parameter: four for θ\theta, three for ϕ\phi. The expanded network formula shows all seven trainable parameters.

ϕ0\phi_0 shifts every prediction equally, so its gradient component is the mean prediction error: −0.1681-0.1681. Parameters remain fixed.

Update all seven parameters together.

θ←θ−α0∇θLB1(θ,ϕ),ϕ←ϕ−α0∇ϕLB1(θ,ϕ)\textcolor{#234e70}{\theta\leftarrow\theta-\alpha_0\nabla_\theta L_{B_1}(\theta,\phi),\qquad\phi\leftarrow\phi-\alpha_0\nabla_\phi L_{B_1}(\theta,\phi)}

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: 0.2129→0.20450.2129\to0.2045.

Next batch. New loss. New gradient.

θ←θ−α1∇θLB2(θ,ϕ),ϕ←ϕ−α1∇ϕLB2(θ,ϕ)\textcolor{#234e70}{\theta\leftarrow\theta-\alpha_1\nabla_\theta L_{B_2}(\theta,\phi),\qquad\phi\leftarrow\phi-\alpha_1\nabla_\phi L_{B_2}(\theta,\phi)}

Take the next 64 observations. Use the learned θ\theta and ϕ\phi 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.

32 batches×64 observations=2,04832\text{ batches}\times64\text{ observations}=2{,}048

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.

1 epoch=1 complete pass through the data1\text{ epoch}=1\text{ complete pass through the data}

Reshuffle the same observations into new batches. Keep the learned weights and update again.

Repeat across 300 epochs.

m=2,αt=0.051+t/6000\textcolor{#234e70}{m=2},\qquad\alpha_t=\frac{0.05}{1+t/6000}

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?

f^(x)=ϕ0+ϕ1ReLU⁡(θ10+θ11x)+ϕ2ReLU⁡(θ20+θ21x)\textcolor{#234e70}{\hat f(x)=\phi_0+\phi_1\operatorname{ReLU}(\theta_{10}+\theta_{11}x)+\phi_2\operatorname{ReLU}(\theta_{20}+\theta_{21}x)}

Hold six fixed; nudge one, then reset. Tiny loss gains leave a broad fit.

1^{1} Full-data MSE evaluates the fit; SGD updates use batches of 64. These trials follow training.

Start again with twelve neurons.

f^(x;θ,ϕ)=ϕ0+∑j=112ϕjReLU⁡(θj0+θj1x)\textcolor{#007f73}{\hat f(x;\theta,\phi)=\phi_0+\sum_{j=1}^{12}\phi_j\operatorname{ReLU}(\theta_{j0}+\theta_{j1}x)}

One hidden layer, twelve neurons: 24 hidden parameters in θ\theta and 13 output parameters in ϕ\phi.

Same data, batch order, and learning rates.

Use the same 32 batches.

θ←θ−αt∇θLB(θ,ϕ),ϕ←ϕ−αt∇ϕLB(θ,ϕ)\textcolor{#007f73}{\theta\leftarrow\theta-\alpha_t\nabla_\theta L_B(\theta,\phi),\qquad\phi\leftarrow\phi-\alpha_t\nabla_\phi L_B(\theta,\phi)}

Still 64 observations and 64 prediction errors. Now 37 gradient components: 24 for θ\theta, 13 for ϕ\phi.

Update all 37 together, using the same current model.

Train the wider network.

m=12,300 epochs=9,600 updates\textcolor{#007f73}{m=12},\qquad300\text{ epochs}=9{,}600\text{ updates}

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.

m=2versusm=12\textcolor{#234e70}{m=2}\qquad\text{versus}\qquad \textcolor{#007f73}{m=12}

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

Different starts. Different fits.

The closing grid shows 8 random initializations for each of two shallow ReLU networks: width 2 and width 12. Each curve is the final fitted function f^(x)\hat f(x); the dashed gray step is the same target f(x)f(x) used throughout the lesson. Every panel uses the same axes. All starts are included, in their original order.

These are separate fits generated with scikit-learn’s MLPRegressor using plain SGD, no momentum, and no regularization. They use the same 2,048 observations, 300 epochs, batches of 64, and learning-rate schedule as the animation. The initial weights use scikit-learn’s default initialization. A separate random stream supplies the same batch order to every fit, so varying the initialization does not also vary the training data or their order. These curves are not replays of the earlier animated runs.

Rust curves show width 2; blue curves show width 12. Training MSE=1n∑i=1n(f^(xi)−yi)2\mathrm{MSE}=\frac{1}{n}\sum_{i=1}^{n}\bigl(\hat f(x_i)-y_i\bigr)^2 is printed above every curve. This is the full mean squared error, twice the half-MSE loss used for the updates.

“Show initialization” adds each network’s function before its first training update as a faint dotted curve in the same color. Every plot expands to the same vertical range so all starting functions remain visible. Turning it off restores the final-fit axes. The solid curves and MSE labels continue to describe the final fits. The starting functions are recovered using the saved random seeds and the recorded software version; their parameters and predictions are stored with the final fits.

A network’s ability to represent a good approximation does not guarantee that training will find it from every starting point. Width 2 can fit the step well, but some starts finish far from it. In these runs the wider network is more consistent, although the best width-two fit has the lowest error. This comparison holds the number of updates fixed, not computation time, and measures training fit rather than performance on unseen data. Every run stops at the same epoch budget; this does not establish convergence.