AI coursesMain page
Back to the library

Gradient descent and backpropagation

Trace an error back to each adjustable coefficient.

On this page
  1. A scalar objective
  2. Updates and convergence conditions
  3. The local descent argument
  4. Batch linear-regression gradients
  5. Backpropagation through a hidden layer
  6. References

A prediction error is a single number, whereas a network may contain many adjustable coefficients. Training needs a way to determine how each coefficient contributes to that error. The gradient supplies the local sensitivities, backpropagation computes them efficiently through shared intermediate calculations, and an optimizer turns them into parameter updates.

Gradient descent updates parameters using a loss derivative. Backpropagation computes derivatives by applying the chain rule through a computation graph. These are distinct operations: one determines gradients, while the other uses them in an update rule.

1. A scalar objective

Consider y^=wx\hat y=wx with fixed x=2x=2 and target y=6y=6. Using squared loss without a factor of 1/21/2 gives:

L(w)=(2w−6)2=4(w−3)2\mathcal L(w)=(2w-6)^2=4(w-3)^2

The derivative follows from the outer square and the inner affine function:

L′(w)=2(2w−6)⋅2=8w−24,L′′(w)=8\mathcal L'(w)=2(2w-6)\cdot2=8w-24, \qquad \mathcal L''(w)=8

At w=1w=1, the loss is 16 and the derivative is −16-16. Increasing ww by a sufficiently small amount therefore lowers the loss; the update calculation below determines how the chosen step size affects the result.

2. Updates and convergence conditions

Gradient descent with fixed learning rate η\eta uses:

wt+1=wt−ηL′(wt)w_{t+1}=w_t-\eta \mathcal L'(w_t)

For w0=1w_0=1 and η=0.1\eta=0.1, this gives w1=2.6w_1=2.6, prediction 5.25.2 and loss 0.640.64. With η=1\eta=1, the update instead gives w1=17w_1=17 and loss 784.

The convergence condition can be derived exactly for this quadratic. Defining the error relative to the minimizer as et=wt−3e_t=w_t-3 yields:

et+1=(1−8η)ete_{t+1}=(1-8\eta)e_t

For a nonzero initial error, convergence requires ∣1−8η∣<1|1-8\eta|<1, equivalent to:

0<η<140<\eta<\frac14

At η=1/8\eta=1/8, the error becomes zero after one update. The stability interval follows from the curvature eight and the specified loss scaling. [1]

3. The local descent argument

For a differentiable scalar objective J(θ)J(\theta) and a small displacement Δ\Delta:

J(θ+Δ)=J(θ)+∇J(θ)⊤Δ+o(∥Δ∥)J(\theta+\Delta) =J(\theta)+\nabla J(\theta)^\top\Delta +o(\|\Delta\|)

Substituting Δ=−η∇J(θ)\Delta=-\eta\nabla J(\theta) makes the linear term −η∥∇J(θ)∥22-\eta\|\nabla J(\theta)\|_2^2. At a nonstationary point this term is negative for η>0\eta>0. The remainder is negligible relative to the displacement only in the local limit, so finite-step behavior still depends on step size. At a stationary point, the first-order argument alone establishes no strict decrease.

4. Batch linear-regression gradients

Let X∈Rn×dX\in\mathbb R^{n\times d}, w∈Rdw\in\mathbb R^d, b∈Rb\in\mathbb R and y∈Rny\in\mathbb R^n. Define:

r=Xw+b1n−y,J(w,b)=12nr⊤rr=Xw+b\mathbf1_n-y,\qquad J(w,b)=\frac{1}{2n}r^\top r

Unlike the scalar example, this objective includes 1/21/2. Expanding the sum of squared residuals and differentiating each term gives:

∂J∂wj=1n∑iriXij,∇wJ=1nX⊤r,∂J∂b=1n1n⊤r\frac{\partial J}{\partial w_j} =\frac1n\sum_i r_iX_{ij},\qquad \nabla_wJ=\frac1nX^\top r,\qquad \frac{\partial J}{\partial b}=\frac1n\mathbf1_n^\top r

Adding ρ∥w∥22/2\rho\|w\|_2^2/2 adds ρw\rho w to the weight gradient. A minibatch version replaces the full-data average with a selected batch average. The differentiation is unchanged in form, while the statistical properties of the estimated gradient depend on the sampling procedure.

5. Backpropagation through a hidden layer

A scalar-output network with mm hidden units can be specified by:

z=Wx+b,a=ReLU⁡(z),y^=v⊤a+c,ℓ=12(y^−y)2z=\mathbf{W}x+b,\quad a=\operatorname{ReLU}(z),\quad \hat y=v^\top a+c,\quad \ell=\frac12(\hat y-y)^2

Here x∈Rdx\in\mathbb R^d, W∈Rm×d\mathbf{W}\in\mathbb R^{m\times d}, b,v∈Rmb,v\in\mathbb R^m, and c,y∈Rc,y\in\mathbb R. Let r=y^−yr=\hat y-y. Differentiation from the output gives:

∇vℓ=ra,∂cℓ=r\nabla_v\ell=ra,\qquad \partial_c\ell=r

Propagating through the activation and first affine layer gives:

δ=(rv)⊙1[z>0],∇Wℓ=δx⊤,∇bℓ=δ,∇xℓ=W⊤δ\delta=(rv)\odot\mathbf1[z>0],\qquad \nabla_\mathbf{W}\ell=\delta x^\top,\quad \nabla_b\ell=\delta,\quad \nabla_x\ell=\mathbf{W}^\top\delta

Here ⊙\odot denotes coordinatewise multiplication. ReLU has no classical derivative at zero; assigning zero to its backward multiplier there is an implementation convention. Away from the kink, the mask is the ordinary derivative. The weight gradient has shape m×dm\times d, as required for an update of W\mathbf{W}.

This reverse accumulation is backpropagation. A rule such as W←W−η∇Wℓ\mathbf{W}\leftarrow \mathbf{W}-\eta\nabla_\mathbf{W}\ell is the subsequent optimization step. During ordinary inference, parameters remain fixed and only the forward computation is required. [2]

References