Backpropagation
Обратное распространение ошибки
This algorithm works out how a small change to each neural network weight would affect the loss. An optimizer uses that information to update the weights.
What goes backward?
A neural network can have millions of weights, but a training step typically produces one loss value: a score that the training process tries to lower. That score alone doesn't tell you which weights to change. Backpropagation computes the gradient of the loss with respect to the trainable parameters. Each component tells you how sensitive the loss is to a small change in one parameter, with the others held fixed.
From the loss back to the weights
On the forward pass, the model uses its current weights to make predictions and evaluate the loss. An automatic differentiation framework such as PyTorch tracks which operations depend on which values and saves intermediates needed for the backward pass.
On the backward pass, the framework starts at the scalar loss and follows those dependencies in reverse. The chain rule tells it how to combine an incoming derivative with the derivative of the current operation. For a chain of scalar operations, that means multiplying the derivatives along the chain. If a value feeds two branches, the backward pass adds their contributions before continuing. Reusing intermediate results this way avoids tracing a separate route from the loss for every weight.
A small branching example
Suppose a = 2*w, b = 3*w, and loss = (a + b - y)**2. With w = 1 and target y = 4, the loss is 1. The derivative with respect to a + b is 2, and both a and b receive that same derivative because each enters the sum with coefficient 1. The two branches contribute 2 * 2 = 4 and 2 * 3 = 6 to the gradient of w, so the result is 10. Missing either branch gives the wrong answer. A basic gradient descent step would subtract the learning rate times 10 from w.
Where it fits in training
Backpropagation computes gradients; it does not choose a learning rate or update weights. In PyTorch, loss.backward() accumulates gradients in .grad, while optimizer.step() uses them to update parameters. Clear gradients with optimizer.zero_grad() before the next backward pass unless you're intentionally combining gradients across batches. Ordinary inference needs only the forward pass because the weights aren't being fitted.
In an interview
You may be asked to derive a gradient for a tiny network or explain why early layers sometimes learn slowly. One possible cause is vanishing gradients: repeatedly multiplying by small derivatives along a long path can leave early layers with almost no training signal. The opposite, exploding gradients, can happen when repeated multiplication amplifies that signal. Be ready to explain the distinction between a large gradient and a large optimizer step: the update also depends on the learning rate and the optimizer's rule.