Backpropagation
Study guide: Exam MAS-II
Backpropagation is the use of the chain rule, from the output of a neural network back to its inputs, to compute the gradient of the loss with respect to every weight in one sweep. For a quantitative response, the loss is
R(\theta) = \frac{1}{2}\sum_{i=1}^{n}\left(y_i - f_\theta(x_i)\right)^2
\thetais the vector of all the network's weights, andf_\theta(x_i)the prediction for observationi.
Gradient descent then moves each weight against its gradient:
\theta \leftarrow \theta - \rho\,\frac{\partial R}{\partial \theta}
\rhois the learning rate.
A learning rate that is too large makes the loss oscillate or diverge, and one that is too small makes training slow.
A weight is
0.40, its gradient is-4and the learning rate is\rho = 0.05. What is the updated weight?
- The step is
-\rho \times (-4) = 0.20.- The updated weight is
0.40 + 0.20 = 0.60.
Forward and backward passes
The forward pass computes each hidden unit's weighted combination and activation, then the output and the loss. The backward pass carries the derivative of the loss from the output back through each layer, reusing each layer's result for the one below. For a weight into hidden unit k, with z_k the unit's weighted combination, the chain rule gives
\frac{\partial R}{\partial w_{kj}} = \frac{\partial R}{\partial A_k}\cdot\frac{\partial A_k}{\partial z_k}\cdot\frac{\partial z_k}{\partial w_{kj}}
The backward pass costs about as much as the forward pass, which is what makes fitting a network with thousands of weights feasible.
Gradient of an output weight
The \frac{1}{2} in the loss cancels the 2 that differentiating the square brings down, so for observation i the gradient of an output weight is
\frac{\partial R_i}{\partial \beta_k} = -\left(y_i - f_\theta(x_i)\right)A_k
A_kis the activation of hidden unitkfor observationi.
The network predicts
3.0for an observation whose response is5.0. The output weight\beta_k = 0.40is fed by an activationA_k = 2.0. What is its gradient, and its value after one step with\rho = 0.05?
- The residual is
5.0 - 3.0 = 2.0.- The gradient is
-(2.0)(2.0) = -4.- The updated weight is
0.40 - 0.05(-4) = 0.60. It rises because the prediction was too low and the activation feeding it is positive.
Gradient of a hidden-layer weight
Carrying the chain rule one layer further, the gradient of a weight into hidden unit k is
\frac{\partial R_i}{\partial w_{kj}} = -\left(y_i - f_\theta(x_i)\right)\beta_k\,g'(z_{ik})\,x_{ij}
g'(z_{ik})is the slope of the activation at the unit's input. For the ReLU it is 1 whenz_{ik} > 0and 0 otherwise.
For one observation the residual is
2.0, the output weight is\beta_k = 1.5, the ReLU unit's input is positive and the predictor isx_ = 2. What is the gradient ofw_?
- The ReLU's slope is 1, because the unit's input is positive.
- The gradient is
-(2.0)(1.5)(1)(2) = -6.- Had the unit's input been negative, its slope would be 0 and the weight would get no gradient from this observation.
Stochastic gradient descent
Stochastic gradient descent applies the update on small batches of observations rather than the whole data set. The loss is non-convex, so different random starting weights give different fits. Regularization comes as a penalty on the weights, early stopping or dropout.
The
\fracin the loss Written without the\frac{1}{2}, as\sum_i (y_i - f_\theta(x_i))^2, the loss has gradients twice as large:-2(y_i - f_\theta(x_i))A_kfor an output weight. Check which loss a question uses before taking a step.