Backpropagation
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A network learns by changing its weights to make the loss smaller, and for that it needs the gradient of the loss for every weight. Backpropagation gets all of them in one sweep: run the network forward, then send the gradient backwards from the loss, multiplying by each part's local slope (the chain rule). Follow one tiny network through a forward pass, a backward pass, a numerical check and one gradient descent step that cuts its loss from 4 to 0.0004.
A tiny network
Our network has one input, x = 1, two weights, w1 = 2 and w2 = 1.5, and one hidden neuron with ReLU: it keeps positive numbers and turns negative ones into 0. We want the output to be 1.
input x = 1 target t = 1 weights w1 = 2 w2 = 1.5 neuron z = w1 * x, h = relu(z) = max(0, z) output y = w2 * h the weights are what training is allowed to change
اقرأ الدرس خطوة بخطوة
A tiny network
Our network has one input, x = 1, two weights, w1 = 2 and w2 = 1.5, and one hidden neuron with ReLU: it keeps positive numbers and turns negative ones into 0. We want the output to be 1.
input x = 1 target t = 1 weights w1 = 2 w2 = 1.5 neuron z = w1 * x, h = relu(z) = max(0, z) output y = w2 * h the weights are what training is allowed to changeThe forward pass
The forward pass runs the network from left to right: z = w1 × x = 2, ReLU keeps it, so h = 2, and y = w2 × h = 3. Each step keeps its numbers, because the backward pass will need them.
z = w1 * x # 2 * 1 = 2 h = max(0.0, z) # relu(2) = 2 y = w2 * h # 1.5 * 2 = 3 remember z, h and y: the backward pass reuses themMeasure the error
The loss says how wrong the output is. We use squared error: (y − t)² = (3 − 1)² = 4, where t is the target. Training means making this number smaller by changing w1 and w2.
loss = (y - t) ** 2 # (3 - 1) ** 2 = 4 y = 3 but we wanted t = 1: the loss is 4 goal: change w1 and w2 so the loss gets smallerWhich weight to change?
For each weight we want its gradient: how fast the loss changes when that weight moves a little. Nudging each weight and rerunning works, but costs a forward pass per weight. Backpropagation finds them all in one backward sweep.
gradient of w1: dw1 = how fast the loss changes when w1 moves a tiny bit nudge every weight: 1 extra forward pass per weight (big networks have millions) backpropagation: 1 backward pass for all of themStart at the loss
Backpropagation starts at the end. The slope of (y − t)² for y is 2(y − t) = 2 × 2 = 4. So if y grew by a tiny amount, the loss would grow by about 4 times that amount.
dy = 2 * (y - t) # 2 * (3 - 1) = 4 dy means: the slope of the loss for y y up by 0.001 -> loss up by about 0.004The chain rule
y = w2 × h, so nudging w2 changes y by h times as much, and nudging h by w2 times. The chain rule multiplies the slopes: dw2 = 4 × h = 8, and h gets 4 × w2 = 6.
dw2 = dy * h # 4 * 2 = 8 dh = dy * w2 # 4 * 1.5 = 6 chain rule: slope along a path = product of the local slopes on it (here dy times dy/dw2 = h)Through the ReLU
ReLU's slope is 1 where its input is positive and 0 where it is negative. Here z = 2 is positive, so the gradient passes straight through: dz = 6. A neuron below zero would pass back nothing.
dz = dh if z > 0 else 0.0 # z = 2 > 0 dz = 6 relu(z) = max(0, z): slope 1 for z > 0, 0 for z < 0 z < 0 would give dz = 0, and w1 would not changeBack to the first weight
z = w1 × x, so dw1 = dz × x = 6 × 1 = 6. Every weight now has its gradient, from one sweep backwards. That is backpropagation: the chain rule, applied from the loss back, reusing each result.
dw1 = dz * x # 6 * 1 = 6 gradients: dw1 = 6 dw2 = 8 each line reused the one before it: nothing was worked out twiceCheck by nudging
We can test it. Nudge w1 from 2 to 2.001 and run forward again: the loss grows from 4 to about 4.006. That is 0.006 for a 0.001 nudge, a slope of about 6, just as backpropagation said.
w1 = 2.001: loss = 4.00600225 (4.00600225 - 4) / 0.001 = 6.00225 backpropagation said dw1 = 6: they agree a gradient check; then w1 goes back to 2Step downhill
Now gradient descent uses the gradients. Each weight moves against its gradient, scaled by a learning rate of 0.1: w1 = 2 − 0.1 × 6 = 1.4 and w2 = 1.5 − 0.1 × 8 = 0.7.
w1 -= lr * dw1 # 2 - 0.1 * 6 = 1.4 w2 -= lr * dw2 # 1.5 - 0.1 * 8 = 0.7 a positive gradient means "this weight raises the loss", so the weight goes down, and the other way roundRun it again
A new forward pass: h = 1.4 and y = 0.7 × 1.4 = 0.98. The loss falls from 4 to 0.0004. Real training repeats forward, backward and update many thousands of times, over many examples.
z = 1.4 * 1 = 1.4 h = 1.4 y = 0.7 * 1.4 = 0.98 loss = (0.98 - 1) ** 2 = 0.0004 (was 4) one training step = forward, backward, update
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01In what order does one training step run?
The forward pass makes a prediction and the loss measures it. The backward pass needs the numbers the forward pass kept, and the update needs the gradients the backward pass found.
02In y = w2 × h, the gradient arriving at y is dy = 4 and h = 2. What is dw2?
Nudging w2 changes y by h times as much, so by the chain rule dw2 = dy × h = 4 × 2 = 8.
03A ReLU neuron's input is z = −3 during the forward pass. What gradient does it pass back to the weight before it?
ReLU is flat (slope 0) for negative inputs, so whatever gradient arrives, it passes back 0, and that weight is not changed by this example.
04Why do neural networks use backpropagation instead of nudging each weight and rerunning the network?
Nudging works (it is how we checked dw1 ≈ 6), but with millions of weights it would need millions of forward passes. One backward pass costs a small, fixed multiple of one forward pass.
05w1 = 2, its gradient is 6, and the learning rate is 0.1. What is w1 after one gradient descent step?
w1 −= lr × dw1 gives 2 − 0.1 × 6 = 1.4. The weight moves against its gradient, because a positive gradient means raising w1 raises the loss.
المسرد
- Forward pass
- Running the network from input to output (and on to the loss), keeping each intermediate number for later.
- Loss
- One number that says how wrong the output is; here the squared error (y − t)².
- Gradient
- For each weight, how fast the loss changes when that weight moves a tiny bit. It says which way, and how strongly, to change the weight.
- Chain rule
- The slope along a chain of steps is the product of each step's own (local) slope.
- Backpropagation
- Working out every weight's gradient by applying the chain rule from the loss backwards through the network, reusing each result once.
- ReLU
- An activation function, max(0, z): it keeps positive numbers and turns negative ones into 0, so its slope is 1 or 0.
- Gradient check
- Testing a computed gradient by nudging one weight a little and measuring how much the loss changes.
- Learning rate
- The number that scales each update: new weight = weight − learning rate × gradient.
الملاحظات والمصادر
- Real networks have many neurons per layer, a bias added in each neuron and millions of weights, and they average the loss over a batch of examples. The rule at each step is the same: multiply the upstream gradient by the local slope.
- At exactly z = 0 ReLU has no slope; software picks 0 there by convention. It almost never matters in practice.
- Backpropagation is reverse-mode automatic differentiation: one backward pass costs a small, fixed multiple of one forward pass, however many weights there are (Baydin et al., 2018).
- Libraries such as PyTorch do the backward pass for you: loss.backward() fills in every weight's gradient using exactly these rules.
- The Python program in the lesson is real: run it and it prints "6.0 8.0" then "1.4 0.7 0.98 0.0004".
- Rumelhart, Hinton & Williams, Learning representations by back-propagating errors, Nature 323 (1986)
- Goodfellow, Bengio & Courville, Deep Learning (MIT Press, 2016), section 6.5: Back-Propagation
- Stanford CS231n notes: Backpropagation, Intuitions
- Stanford CS231n notes: Neural Networks Part 3, gradient checks
- Stanford CS229 lecture notes (A. Ng, T. Ma): least squares and gradient descent
- Baydin, Pearlmutter, Radul & Siskind, Automatic Differentiation in Machine Learning: a Survey, JMLR 18 (2018)
- Nair & Hinton, Rectified Linear Units Improve Restricted Boltzmann Machines, ICML 2010
- PyTorch tutorials: A Gentle Introduction to torch.autograd
اختبار يتاح بعد الخطوة الأخيرة.