Deep Learning

Backpropagation & empirical risk

This lecture trains a small network by hand with backpropagation, from feedforward sums to gradient updates. It defines the MSE loss and empirical risk as the average loss over training samples. After this page you can do one full backpropagation update with learning rate 0.1 by hand.

Training with backpropagationSearch on YouTube

Training finds the best learnable parameters of a network. It repeats three steps: feedforward, loss calculation, and backpropagation. Each cycle nudges every weight and bias. Many cycles drive the loss down.

Feedforward alone only predicts. The loss scores the prediction against the label. Backpropagation assigns credit to each weight for that score. Without this loop the network cannot learn.

Feed, Loss, Push: F-L-P

Feedforward sends inputs forward · Loss scores the prediction · Push gradients back and update each weight.

Feedforward passSearch on YouTube

Feedforward sends the input vector through the network layer by layer. Each neuron forms a weighted sum of its inputs plus a bias. Then it applies its activation function. The slides write the hidden output as a=f(z)a = f(z) and pass it on as h=ah = a. They write the final output as O=F(z)O = F(z).

z=∑iwixi+ba=f(z)h=aO=F(z)z = \sum_i w_i x_i + b \qquad a = f(z) \qquad h = a \qquad O = F(z)

zz = weighted sum plus bias at one neuron. wiw_i = weight on input xix_i. bb = bias of the neuron. ff and FF = activation functions. OO = network output.

Hidden-layer sumsSearch on YouTube

The hidden layer converts raw inputs into learned features. Each hidden neuron has its own weights and bias. Its output becomes an input of the next layer.

Output-layer sumSearch on YouTube

The output layer combines the hidden outputs into one sum. It adds its own bias and applies its own activation function. That result is the prediction OO.

x1 x2 h1 h2 O
The 2-2-1 network of the worked example. Each arrow carries a weight and each neuron adds a bias.

Worked example. Inputs x1=0.5x_1 = 0.5 and x2=0.3x_2 = 0.3. Hidden neuron 1 uses weights 0.40.4 and 0.20.2 with bias 0.10.1. Hidden neuron 2 uses weights 0.30.3 and 0.50.5 with bias −0.1-0.1. The output neuron uses weights 0.60.6 and 0.40.4 with bias 0.10.1. Every activation is the sigmoid σ\sigma. The label is y=1y = 1.

Step 1. Hidden weighted sums.

zh1=0.4(0.5)+0.2(0.3)+0.1=0.36zh2=0.3(0.5)+0.5(0.3)−0.1=0.20\begin{aligned} z_{h1} &= 0.4(0.5) + 0.2(0.3) + 0.1 = 0.36\\ z_{h2} &= 0.3(0.5) + 0.5(0.3) - 0.1 = 0.20 \end{aligned}

Step 2. Hidden activations. Apply σ(z)=1/(1+e−z)\sigma(z) = 1/(1+e^{-z}).

h1=σ(0.36)=0.5890h2=σ(0.20)=0.5498\begin{aligned} h_1 &= \sigma(0.36) = 0.5890\\ h_2 &= \sigma(0.20) = 0.5498 \end{aligned}

Step 3. Output sum and prediction.

zo=0.6(0.5890)+0.4(0.5498)+0.1=0.6734O=σ(0.6734)=0.6623\begin{aligned} z_o &= 0.6(0.5890) + 0.4(0.5498) + 0.1 = 0.6734\\ O &= \sigma(0.6734) = \mathbf{0.6623} \end{aligned}

Loss calculationSearch on YouTube

Loss measures the gap between the prediction and the label for one sample. The slides use the mean squared error (MSE). With one sample (n=1n = 1) the mean is the single squared gap.

L(y,O)=1n∑i=1n(yi−Oi)2L(y, O) = \frac{1}{n}\sum_{i=1}^{n}(y_i - O_i)^2

yiy_i = label of sample ii. OiO_i = network prediction for sample ii. nn = number of samples. Square the gap so positive and negative gaps both add cost.

Worked example. The network above predicts O=0.6623O = 0.6623 for label y=1y = 1 (n=1n = 1).

L=(1−0.6623)2=(0.3377)2=0.1141\begin{aligned} L &= (1 - 0.6623)^2\\ &= (0.3377)^2\\ &= \mathbf{0.1141} \end{aligned}

Worked example. Two samples. Labels 11 and 00. Predictions 0.70.7 and 0.30.3.

L1=(1−0.7)2=0.09L2=(0−0.3)2=0.09L=(0.09+0.09)/2=0.09\begin{aligned} L_1 &= (1 - 0.7)^2 = 0.09\\ L_2 &= (0 - 0.3)^2 = 0.09\\ L &= (0.09 + 0.09)/2 = \mathbf{0.09} \end{aligned}

Backpropagation at the output layerSearch on YouTube

Backpropagation sends the loss signal backward through the network. The chain rule splits each gradient into a product of local partials. The output layer comes first because it sits next to the loss.

σ′(z)=σ(z)(1−σ(z))\sigma^{\prime}(z) = \sigma(z)(1 - \sigma(z))

The slides call this Equation (1). It gives the slope of the sigmoid at any sum zz. Every sigmoid partial below uses it.

δo=∂L∂zo=−2(y−O) O(1−O)w:=w−η ∂L∂w\delta_o = \frac{\partial L}{\partial z_o} = -2(y - O)\,O(1 - O) \qquad w := w - \eta\,\frac{\partial L}{\partial w}

δo\delta_o = loss signal at the output sum. η\eta = learning rate (here η=0.1\eta = 0.1). Each weight moves against its gradient. The bias updates the same way.

Worked example. Loss L=0.1141L = 0.1141, O=0.6623O = 0.6623, y=1y = 1, η=0.1\eta = 0.1.

Step 1. Output loss signal.

∂L∂O=−2(1−0.6623)=−0.6755∂O∂zo=0.6623(1−0.6623)=0.2237δo=−0.6755×0.2237=−0.1511\begin{aligned} \frac{\partial L}{\partial O} &= -2(1 - 0.6623) = -0.6755\\ \frac{\partial O}{\partial z_o} &= 0.6623(1 - 0.6623) = 0.2237\\ \delta_o &= -0.6755 \times 0.2237 = \mathbf{-0.1511} \end{aligned}

Step 2. Output-layer gradients. Each gradient is the signal times its input.

∂L∂v1=δoh1=−0.1511×0.5890=−0.0890∂L∂v2=δoh2=−0.1511×0.5498=−0.0831∂L∂c=δo=−0.1511\begin{aligned} \frac{\partial L}{\partial v_1} &= \delta_o h_1 = -0.1511 \times 0.5890 = -0.0890\\ \frac{\partial L}{\partial v_2} &= \delta_o h_2 = -0.1511 \times 0.5498 = -0.0831\\ \frac{\partial L}{\partial c} &= \delta_o = -0.1511 \end{aligned}

Step 3. Update with η=0.1\eta = 0.1. Subtract the learning rate times each gradient.

v1:=0.6−0.1(−0.0890)=0.6089v2:=0.4−0.1(−0.0831)=0.4083c:=0.1−0.1(−0.1511)=0.1151\begin{aligned} v_1 &:= 0.6 - 0.1(-0.0890) = \mathbf{0.6089}\\ v_2 &:= 0.4 - 0.1(-0.0831) = \mathbf{0.4083}\\ c &:= 0.1 - 0.1(-0.1511) = \mathbf{0.1151} \end{aligned}

Keep the sign. A negative gradient raises the weight because minus times minus is plus. Check the sign of δo\delta_o before every update.

Backpropagation at the hidden layerSearch on YouTube

The hidden layer needs the signal that passes through the output weights. Each hidden neuron gets only its share of δo\delta_o. Then the sigmoid slope at its own sum shapes that share.

δh1=δo v1 h1(1−h1)δh2=δo v2 h2(1−h2)\delta_{h1} = \delta_o\,v_1\,h_1(1 - h_1) \qquad \delta_{h2} = \delta_o\,v_2\,h_2(1 - h_2)

δh\delta_{h} = loss signal at a hidden sum. vv = output-layer weight on that hidden output. h(1−h)h(1-h) = sigmoid slope from Equation (1). Hidden weights update with w:=w−η δhxw := w - \eta\,\delta_h x.

Worked example. Signal δo=−0.1511\delta_o = -0.1511 flows back through v1=0.6v_1 = 0.6 and v2=0.4v_2 = 0.4.

Step 1. Hidden loss signals.

δh1=−0.1511×0.6×0.5890×0.4110=−0.0219δh2=−0.1511×0.4×0.5498×0.4502=−0.0150\begin{aligned} \delta_{h1} &= -0.1511 \times 0.6 \times 0.5890 \times 0.4110 = \mathbf{-0.0219}\\ \delta_{h2} &= -0.1511 \times 0.4 \times 0.5498 \times 0.4502 = \mathbf{-0.0150} \end{aligned}

Step 2. Hidden gradients. Multiply each signal by its input (x1=0.5x_1 = 0.5, x2=0.3x_2 = 0.3).

∂L/∂w11=−0.0219×0.5=−0.0110∂L/∂w21=−0.0219×0.3=−0.0066∂L/∂w12=−0.0150×0.5=−0.0075∂L/∂w22=−0.0150×0.3=−0.0045\begin{aligned} \partial L/\partial w_{11} &= -0.0219 \times 0.5 = -0.0110\\ \partial L/\partial w_{21} &= -0.0219 \times 0.3 = -0.0066\\ \partial L/\partial w_{12} &= -0.0150 \times 0.5 = -0.0075\\ \partial L/\partial w_{22} &= -0.0150 \times 0.3 = -0.0045 \end{aligned}

Step 3. Update with η=0.1\eta = 0.1.

w11:=0.4−0.1(−0.0110)=0.4011w21:=0.2−0.1(−0.0066)=0.2007b1:=0.1−0.1(−0.0219)=0.1022w12:=0.3−0.1(−0.0075)=0.3007w22:=0.5−0.1(−0.0045)=0.5004b2:=−0.1−0.1(−0.0150)=−0.0985\begin{aligned} w_{11} &:= 0.4 - 0.1(-0.0110) = \mathbf{0.4011}\\ w_{21} &:= 0.2 - 0.1(-0.0066) = \mathbf{0.2007}\\ b_1 &:= 0.1 - 0.1(-0.0219) = \mathbf{0.1022}\\ w_{12} &:= 0.3 - 0.1(-0.0075) = \mathbf{0.3007}\\ w_{22} &:= 0.5 - 0.1(-0.0045) = \mathbf{0.5004}\\ b_2 &:= -0.1 - 0.1(-0.0150) = \mathbf{-0.0985} \end{aligned}

Update the biases too. Students often update the weights and leave the biases fixed. The bias gradient equals the neuron signal, so its step is η δ\eta\,\delta.

Second feedforward passSearch on YouTube

One update ends one training round. A fresh feedforward pass with the new weights tests the round. The loss should fall.

Worked example. Use all six updated weights and both updated biases.

Step 1. New hidden outputs.

zh1=0.4011(0.5)+0.2007(0.3)+0.1022=0.3629h1=σ(0.3629)=0.5898zh2=0.3007(0.5)+0.5004(0.3)−0.0985=0.2020h2=σ(0.2020)=0.5503\begin{aligned} z_{h1} &= 0.4011(0.5) + 0.2007(0.3) + 0.1022 = 0.3629\\ h_1 &= \sigma(0.3629) = 0.5898\\ z_{h2} &= 0.3007(0.5) + 0.5004(0.3) - 0.0985 = 0.2020\\ h_2 &= \sigma(0.2020) = 0.5503 \end{aligned}

Step 2. New prediction and loss.

zo=0.6089(0.5898)+0.4083(0.5503)+0.1151=0.6989O=σ(0.6989)=0.6679L=(1−0.6679)2=0.1103\begin{aligned} z_o &= 0.6089(0.5898) + 0.4083(0.5503) + 0.1151 = 0.6989\\ O &= \sigma(0.6989) = 0.6679\\ L &= (1 - 0.6679)^2 = \mathbf{0.1103} \end{aligned}

Step 3. Compare. Loss falls from 0.11410.1141 to 0.1103\mathbf{0.1103}. The update helps. Many more rounds follow.

The lab below replays the worked example on this page. It moves the loss signal back and updates each weight once.

0.40.30.20.50.60.4x10.5x20.3h10.5890h20.5498O0.6623L = 0.1141

The 2-2-1 network learns one sample with label 1. Circles hold activations and arrows hold weights.

Feedforward gives O = 0.6623 and loss L = 0.1141. Select a circle or an arrow. Run plays one update with learning rate 0.1.

Batch, SGD, and minibatchSearch on YouTube

The update rule needs gradients, but the data size sets how often updates happen. Three schemes split the same epoch in different ways. One epoch means one full pass over the training set.

MethodSamples per updateUpdates per epochCharacter
Batch GDall nn11stable gradient, slow per step
SGD11nnnoisy steps, fast and frequent
MinibatchBB (1<B<n1 \lt B \lt n)n/Bn/Bmiddle ground, used in practice

Batch GD sums the gradient over every sample before one step. SGD updates after each single sample. Minibatch updates after each small group of BB samples. Minibatch keeps some noise but stays efficient on modern hardware. The slides name all three as ways to minimize empirical risk.

Chain-rule equationsSearch on YouTube

The chain rule links the loss to any weight through the sums between them. Each factor is a local slope. Their product is the full gradient.

∂L∂w=∂L∂O ∂O∂z ∂z∂w∂z∂w=x\frac{\partial L}{\partial w} = \frac{\partial L}{\partial O}\,\frac{\partial O}{\partial z}\,\frac{\partial z}{\partial w} \qquad \frac{\partial z}{\partial w} = x

∂L/∂O\partial L/\partial O = loss slope at the prediction. ∂O/∂z\partial O/\partial z = activation slope (use Equation (1) for sigmoid). ∂z/∂w\partial z/\partial w = the input xx that the weight scales. Deeper weights chain more factors.

Empirical risk minimizationSearch on YouTube

Training minimizes the average loss on the training set. This average has a name: empirical risk. The theory behind it starts with the data distribution.

Joint data distributionSearch on YouTube

Supervised data holds pairs (X,y)(X, y). Here XX is an input feature vector and yy is its label. A joint probability distribution P(X,y)P(X, y) governs how these pairs arise. It states the chance of each input and label happening together. It acts like the real-world rule that produces labels for inputs.

The true distribution stays unknown. The training set is only a finite sample drawn from it. The goal is a function fθf_{\theta} that mimics the true map from XX to yy. Here θ\theta is the parameter vector holding all weights and biases. A good fθf_{\theta} works on new samples, not only on training samples.

Empirical riskSearch on YouTube

R(θ)=1n∑i=1nL(yi,f(Xi,θ))R(\theta) = \frac{1}{n}\sum_{i=1}^{n}L(y_i, f(X_i, \theta))

R(θ)R(\theta) = empirical risk for parameters θ\theta. LL = per-sample loss. f(Xi,θ)f(X_i, \theta) = prediction, which depends on both the input vector and the parameters. Empirical risk is the average loss over all training samples. Training minimizes it with gradient descent, SGD, or momentum methods.

Worked example. Four samples give losses 0.040.04, 0.090.09, 0.160.16, and 0.250.25.

R=(0.04+0.09+0.16+0.25)/4=0.54/4=0.135\begin{aligned} R &= (0.04 + 0.09 + 0.16 + 0.25)/4\\ &= 0.54/4\\ &= \mathbf{0.135} \end{aligned}

The single number 0.1350.135 scores the whole training set. The optimizer drives it down step by step.

Do not trust a low training loss alone. A model can memorize training samples yet fail on new data. Low empirical risk must still generalize to the true distribution.

Quick self-testSearch on YouTube

1. Name the three training steps in order.

Feedforward, loss calculation, backpropagation. Repeat the three to lower the loss.

2. Inputs are 0.5 and 0.3. A neuron has weights 0.4 and 0.2 with bias 0.1. Find z and h with a sigmoid.

z=0.4(0.5)+0.2(0.3)+0.1=0.36z = 0.4(0.5) + 0.2(0.3) + 0.1 = 0.36. h=σ(0.36)=0.5890h = \sigma(0.36) = 0.5890.

3. Label is 1 and prediction is 0.6623. Find the MSE loss for n = 1.

L=(1−0.6623)2=0.1141L = (1 - 0.6623)^2 = 0.1141. One sample means the mean equals the single squared gap.

4. Given output signal -0.1511 and hidden output 0.5890, find the gradient for their connecting weight.

Multiply signal by input: −0.1511×0.5890=−0.0890-0.1511 \times 0.5890 = -0.0890. With η=0.1\eta = 0.1 the weight rises by 0.00890.0089.

5. Why does the hidden signal multiply by h(1 - h)?

h(1−h)h(1-h) is the sigmoid slope from Equation (1). It converts the signal at the hidden output into a signal at the hidden sum.

6. Differentiate batch GD, SGD, and minibatch by updates per epoch.

Batch GD makes 11 update per epoch. SGD makes nn updates. Minibatch makes n/Bn/B updates. Minibatch is the practical middle ground.

7. A weight is 0.4 and its gradient is -0.0110. Update it with learning rate 0.1.

w:=0.4−0.1(−0.0110)=0.4011w := 0.4 - 0.1(-0.0110) = 0.4011. The negative gradient raises the weight.

8. Four losses are 0.04, 0.09, 0.16, 0.25. Find the empirical risk.

R=0.54/4=0.135R = 0.54/4 = 0.135. Empirical risk is the average loss over training samples.

9. After one update the loss falls from 0.1141 to 0.1103. Does training stop now?

No. One step only starts the process. Training repeats feedforward, loss, and backpropagation until the loss settles.

10. Training loss is near zero but new data scores poorly. What went wrong?

The model fits training samples without matching the true distribution. Low empirical risk alone does not guarantee new-data results.

  • Training repeats three steps: feedforward, loss calculation, backpropagation.
  • Feedforward builds z=∑wixi+bz = \sum w_i x_i + b, then a=f(z)a = f(z) with h=ah = a, and O=F(z)O = F(z).
  • MSE loss is L=(1/n)∑(yi−Oi)2L = (1/n)\sum (y_i - O_i)^2; the example gives 0.11410.1141.
  • Sigmoid slope (Equation 1) is σ′(z)=σ(z)(1−σ(z))\sigma^{\prime}(z) = \sigma(z)(1-\sigma(z)).
  • Output signal is δo=−2(y−O)O(1−O)\delta_o = -2(y-O)O(1-O); weights move by w:=w−η ∂L/∂ww := w - \eta\,\partial L/\partial w with η=0.1\eta = 0.1.
  • Hidden signals are δh=δovh(1−h)\delta_h = \delta_o v h(1-h); biases update too.
  • The second feedforward pass cuts the loss from 0.11410.1141 to 0.11030.1103.
  • Batch GD uses all nn samples per update, SGD uses 11, minibatch uses BB.
  • Empirical risk is R(θ)=(1/n)∑L(yi,f(Xi,θ))R(\theta) = (1/n)\sum L(y_i, f(X_i, \theta)); the example gives 0.1350.135.
  • Low training loss alone does not guarantee good results on new data.