1. Home
  2. Explore
  3. Computer Science
  4. Backpropagation in Neural Networks

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Backpropagation in Neural Networks

Backpropagation and Computation Graphs

Why gradients are needed

Training a model with stochastic gradient descent (SGD) requires the gradient of its loss with respect to each trainable parameter. For a neural network, these parameters include the weight matrices in its layers and the final output weights.

A network computes its prediction through a sequence of operations: it transforms an input feature vector with weight matrices, applies activation functions, and eventually produces a score or prediction. The loss compares that prediction with the target. Although the gradient of the loss with respect to each parameter can be found by repeatedly applying calculus rules, doing so by hand becomes tedious as networks grow more complex.

Backpropagation automates this gradient calculation. It works by representing a mathematical expression as a computation graph and applying the chain rule systematically.


Computation graphs

A computation graph is a directed acyclic graph (DAG) that represents how a mathematical expression is built from smaller operations.

  • Input nodes represent values such as features, targets, and parameters.

  • Operation nodes represent functions such as addition, multiplication, or an activation.

  • Intermediate nodes hold the results of subexpressions.

  • The output node represents the final expression, such as a loss.

For example, the expression c=a+bc=a+bc=a+b can be represented by a graph in which aaa and bbb flow into an addition operation, producing ccc. A more complex model can be represented by connecting many such operations together.

Computation graphs are useful for two related reasons:

  1. They allow software to calculate gradients automatically. Deep-learning systems use this general approach to differentiate models.

  2. They make the structure of a calculation visible, helping explain how each part contributes to the final gradient.

A graph also reveals shared subexpressions. If multiple computations depend on the same intermediate value, their gradient contributions can be combined rather than recomputed independently.


Local derivatives as edge values

Each operation in a computation graph has a local derivative: the derivative of its output with respect to one of its inputs. This derivative describes how a small change in that input changes the output.

For example, if c=a+bc=a+bc=a+b, increasing aaa by a small amount ϵ\epsilonϵ increases ccc by the same amount. Thus, ∂c∂a=1\frac{\partial c}{\partial a}=1∂a∂c​=1. The same reasoning gives ∂c∂b=1\frac{\partial c}{\partial b}=1∂b∂c​=1.

The local derivatives for common operations are:

Addition: c=a+bc=a+bc=a+b

  • ∂c∂a=1\frac{\partial c}{\partial a}=1∂a∂c​=1

  • ∂c∂b=1\frac{\partial c}{\partial b}=1∂b∂c​=1

Subtraction: c=a−bc=a-bc=a−b

  • ∂c∂a=1\frac{\partial c}{\partial a}=1∂a∂c​=1

  • ∂c∂b=−1\frac{\partial c}{\partial b}=-1∂b∂c​=−1

Increasing the subtracted input bbb decreases the output, which accounts for the negative derivative.

Multiplication: c=abc=abc=ab

  • ∂c∂a=b\frac{\partial c}{\partial a}=b∂a∂c​=b

  • ∂c∂b=a\frac{\partial c}{\partial b}=a∂b∂c​=a

Each input’s effect is scaled by the other input.

Squaring: c=a2c=a^2c=a2

  • dcda=2a\frac{dc}{da}=2adadc​=2a

This is the power rule for differentiation.

Maximum: c=max⁡(a,b)c=\max(a,b)c=max(a,b)

Away from the point where a=ba=ba=b:

  • ∂c∂a=1\frac{\partial c}{\partial a}=1∂a∂c​=1 if a>ba>ba>b, and 000 if a<ba<ba<b.

  • ∂c∂b=1\frac{\partial c}{\partial b}=1∂b∂c​=1 if b>ab>ab>a, and 000 if b<ab<ab<a.

Only the input that determines the maximum affects the output locally. At a=ba=ba=b, the maximum function is not differentiable in the ordinary sense; an optimization algorithm can use a valid subgradient there.

Logistic sigmoid

The logistic sigmoid is

σ(a)=11+e−a.\sigma(a)=\frac{1}{1+e^{-a}}.σ(a)=1+e−a1​.

Its derivative has the convenient form

dσ(a)da=σ(a)(1−σ(a)).\frac{d\sigma(a)}{da}=\sigma(a)\bigl(1-\sigma(a)\bigr).dadσ(a)​=σ(a)(1−σ(a)).

As aaa becomes very large or very negative, the sigmoid approaches 111 or 000, respectively. Its derivative then approaches 000, illustrating how an activation can produce a very small gradient in saturated regions.


The chain rule on a graph

When one operation feeds into another, their derivatives combine through the chain rule. In a computation graph, the derivative along a path is the product of the local derivatives on that path.

For example, let

b=a2,c=b2.b=a^2,\qquad c=b^2.b=a2,c=b2.

The local derivatives are

∂b∂a=2a,∂c∂b=2b.\frac{\partial b}{\partial a}=2a, \qquad \frac{\partial c}{\partial b}=2b.∂a∂b​=2a,∂b∂c​=2b.

Therefore,

∂c∂a=∂c∂b∂b∂a=2b⋅2a.\frac{\partial c}{\partial a} = \frac{\partial c}{\partial b} \frac{\partial b}{\partial a} = 2b\cdot 2a.∂a∂c​=∂b∂c​∂a∂b​=2b⋅2a.

Since b=a2b=a^2b=a2, this becomes

∂c∂a=4a3.\frac{\partial c}{\partial a}=4a^3.∂a∂c​=4a3.

Because c=(a2)2=a4c=(a^2)^2=a^4c=(a2)2=a4, the result agrees with the power rule. This illustrates the central graph-based principle: multiply local derivatives along a path to obtain the derivative through that path.

When a value affects the output through multiple paths, the contributions from those paths must be added.


Example: hinge loss for linear classification

Let ϕ(x)\phi(x)ϕ(x) be the feature vector for an input xxx, let www be a weight vector, and let yyy be the label. A linear classifier computes the score

f(x)=w⊤ϕ(x).f(x)=w^\top\phi(x).f(x)=w⊤ϕ(x).

The margin is yf(x)y f(x)yf(x), and the hinge loss is

L=max⁡(0, 1−yw⊤ϕ(x)).L=\max\bigl(0,\,1-yw^\top\phi(x)\bigr).L=max(0,1−yw⊤ϕ(x)).

The computation proceeds through these intermediate values:

  1. Compute the score w⊤ϕ(x)w^\top\phi(x)w⊤ϕ(x).

  2. Multiply the score by yyy to obtain the margin.

  3. Subtract the margin from 111.

  4. Take the maximum of that result and 000.

If the margin is greater than 111, the loss is 000 and its gradient is 000. If the margin is less than 111, the loss is active, and the gradient with respect to www is

∇wL=−yϕ(x).\nabla_w L=-y\phi(x).∇w​L=−yϕ(x).

Together, away from the boundary, this is

∇wL=−1{yw⊤ϕ(x)<1} yϕ(x),\nabla_w L = -\mathbf{1}\{yw^\top\phi(x)<1\}\,y\phi(x),∇w​L=−1{yw⊤ϕ(x)<1}yϕ(x),

where 1{⋅}\mathbf{1}\{\cdot\}1{⋅} is 111 when its condition is true and 000 otherwise. At a margin exactly equal to 111, the hinge loss is nondifferentiable, so a subgradient is used.

The graph makes this result systematic: the local derivatives are combined along the path from the loss back to the weights.


Example: a two-layer neural network

Consider a neural network that computes a hidden representation, then a score, then a squared loss. Let xxx be an input, ϕ(x)\phi(x)ϕ(x) its feature vector, VVV the hidden-layer weight matrix, and www the output weights. Define

z=Vϕ(x),h=σ(z),s=w⊤h,r=s−y,L=r2.z=V\phi(x), \qquad h=\sigma(z), \qquad s=w^\top h, \qquad r=s-y, \qquad L=r^2.z=Vϕ(x),h=σ(z),s=w⊤h,r=s−y,L=r2.

Here, σ\sigmaσ is applied elementwise, hhh is the hidden vector, sss is the score, and rrr is the residual.

Gradient with respect to the output weights

The derivative of the squared loss with respect to the residual is 2r2r2r. The residual changes one-for-one with the score, and the score’s derivative with respect to www is hhh. Thus,

∇wL=2rh.\nabla_w L=2r h.∇w​L=2rh.

This has the same form as the gradient for squared-loss linear regression, with the hidden representation hhh playing the role of the feature vector.

Gradient with respect to the hidden-layer weights

The gradient flows backward from the loss through the score, the activation, and the matrix multiplication. For the sigmoid, the elementwise derivative is h⊙(1−h)h\odot(1-h)h⊙(1−h), where ⊙\odot⊙ denotes elementwise multiplication. Therefore,

∇VL=(2r w⊙h⊙(1−h))ϕ(x)⊤.\nabla_V L = \left(2r\,w\odot h\odot(1-h)\right)\phi(x)^\top.∇V​L=(2rw⊙h⊙(1−h))ϕ(x)⊤.

The expression in parentheses is the error signal propagated to the hidden units; its outer product with ϕ(x)⊤\phi(x)^\topϕ(x)⊤ gives the gradient for the matrix VVV.

The same principles apply to deeper networks. Additional layers add more operations and local derivatives to the paths, but do not change the underlying chain-rule procedure.


Forward and backward passes

Backpropagation calculates gradients in two passes through the computation graph.

Forward pass: evaluate and store values

The forward pass moves from the input nodes toward the output. It computes the value of every intermediate expression and stores those values. They are needed later to evaluate local derivatives.

For a concrete squared-loss example, let

w=[31],x=[12],y=2.w= \begin{bmatrix} 3\\ 1 \end{bmatrix}, \qquad x= \begin{bmatrix} 1\\ 2 \end{bmatrix}, \qquad y=2.w=[31​],x=[12​],y=2.

The score, residual, and loss are

s=w⊤x=3(1)+1(2)=5,s=w^\top x=3(1)+1(2)=5,s=w⊤x=3(1)+1(2)=5,
r=s−y=5−2=3,r=s-y=5-2=3,r=s−y=5−2=3,
L=r2=9.L=r^2=9.L=r2=9.

The forward pass stores the intermediate values s=5s=5s=5, r=3r=3r=3, and L=9L=9L=9.

Backward pass: propagate derivatives

The backward pass moves from the loss toward the inputs and parameters. For each node, it computes a backward value, also called an adjoint: the derivative of the final loss with respect to the value at that node.

At the loss node,

∂L∂L=1.\frac{\partial L}{\partial L}=1.∂L∂L​=1.

For L=r2L=r^2L=r2,

∂L∂r=2r=6.\frac{\partial L}{\partial r}=2r=6.∂r∂L​=2r=6.

Since r=s−yr=s-yr=s−y, the derivative of rrr with respect to sss is 111, so

∂L∂s=6.\frac{\partial L}{\partial s}=6.∂s∂L​=6.

Finally, since s=w⊤xs=w^\top xs=w⊤x, the gradient with respect to www is

∇wL=6x=[612].\nabla_w L=6x= \begin{bmatrix} 6\\ 12 \end{bmatrix}.∇w​L=6x=[612​].

Thus, the backward pass uses both the local derivatives and the stored forward values to calculate the gradient at the parameters.

In a general graph, each node passes gradient contributions to its inputs. If a node’s value contributes to the loss through multiple downstream paths, those contributions are summed. For vector- and matrix-valued operations, the same chain rule applies, with the relevant derivatives represented by Jacobians or equivalent vectorized calculations.


Using gradients to train models

Once backpropagation has computed gradients, SGD uses them to update parameters. For a parameter vector θ\thetaθ, a basic update is

θ←θ−η∇θL,\theta \leftarrow \theta-\eta\nabla_\theta L,θ←θ−η∇θ​L,

where η\etaη is the step size, or learning rate. The update moves parameters in the direction that locally reduces the loss.

For a convex training objective, suitable optimization conditions can provide guarantees that gradient-based methods approach a global minimum. Many linear-model objectives with convex losses have this property. Neural-network training objectives, by contrast, are generally non-convex. They can contain multiple local minima and other difficult regions, so there is generally no guarantee that gradient descent will reach a global minimum.

Despite this theoretical difficulty, neural networks often train successfully in practice. Their optimization can require careful choices, and training behavior may depend strongly on the model and procedure.

Initialization

The starting parameter values can influence where optimization goes in a non-convex loss landscape. A poor initialization may lead to slow progress or an unfavorable solution.

Initializing all weights to zero is generally problematic for a multilayer network: units within a layer can become identical and receive identical updates, preventing them from learning distinct features. Random initialization breaks this symmetry. Pre-training can also be used to produce starting weights, though it is a separate technique.

Overparameterization

Overparameterization means providing a model with more hidden units or parameters than may seem strictly necessary. Some units may contribute little, while others may learn useful patterns. The extra capacity can give optimization more opportunities to find a useful representation, although it does not by itself guarantee successful training.

Adaptive step sizes

A single learning rate applies the same nominal step-size scale to every parameter. Adaptive methods such as AdaGrad and Adam instead adjust step sizes by parameter, using information from the history of gradients. This can improve optimization when different parameters require different update scales.

Vanishing and exploding gradients

A gradient that becomes extremely small is a vanishing gradient. It can produce tiny parameter updates and prevent early layers from learning effectively. A gradient that becomes extremely large is an exploding gradient. It can cause unstable updates, oscillation, or divergence.

Gradient behavior is influenced by weight initialization, the choice of step sizes, activation functions, and network architecture. These choices are made in part to keep gradients at scales that permit learning without causing instability.


Core ideas

  • A computation graph represents a function as connected elementary operations.

  • Local derivatives describe how each operation responds to changes in its inputs.

  • The chain rule combines local derivatives: contributions multiply along paths and add across separate paths.

  • The forward pass evaluates the function and stores intermediate values; the backward pass uses them to compute gradients.

  • Backpropagation is not limited to neural networks. It applies broadly to differentiable computations and allows models and loss functions to be changed without deriving every gradient manually.

  • Automatic differentiation through computation graphs supports a programming approach in which models and objectives can be expressed as compositions of operations, then differentiated automatically.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent