Create your own
Lesson illustration

Using the Chain Rule for Gradient Descent

Hello. In the previous lesson, you learned that the gradient of a scalar loss points uphill and that gives the local downhill direction. The remaining practical question is: how do we compute the loss gradient when a model is a sequence of many operations?

This lesson answers that question with the chain rule and a computational graph. You will trace a forward pass through a one-neuron model, propagate derivatives backward to its parameters, and apply a gradient-descent update. This is the small-scale version of backpropagation used in every differentiable neural network.


From a nested expression to a computational graph

Consider a simple model for one training example:

Here:

  • is an input feature and is its target;
  • and are trainable weight and bias;
  • is the pre-activation;
  • is the prediction after the sigmoid activation;
  • is a scalar loss.

Written as one expression, the loss is:

You could differentiate this directly. But neural networks make such expressions far too large to manipulate reliably as one formula. Instead, break the computation into local operations:

A computational graph represents exactly this decomposition:

  • nodes are values, such as , , and ;
  • edges/operations show how one value is computed from earlier values;
  • the graph is evaluated left to right during the forward pass;
  • gradients are computed right to left during the backward pass.
A computational graph for a one-feature neuron: the forward pass computes \(u=w_1x_1\), then \(z=u+b\), activation \(a=\sigma(z)\), and loss \(L(a,y)\). The backward arrows represent the chain-rule derivatives used to obtain gradients for the trainable weight and bias.

The image’s central idea is worth stating precisely:

A parameter influences the loss through the intermediate values between them. The chain rule multiplies the local sensitivities along that path.

For the weight , the dependency path is:

Therefore,

For the bias,

The loss-to-activation and activation-to-pre-activation factors are shared. Backpropagation exploits this reuse rather than deriving a separate expanded expression for every parameter.

Backpropagation calculus | Deep Learning Chapter 4

Watch “Backpropagation calculus” from 3Blue1Brown for a visual derivation of the chain rule through a single-neuron network. It closely matches the model structure used in this lesson.

Watch the weight gradient, where the loss sensitivity is decomposed into the change from weight to pre-activation, pre-activation to activation, and activation to cost. Then watch biases and backward flow to see why the same calculation reaches earlier quantities in the graph. Focus on what each derivative means physically: “if this quantity changes slightly, how much does the final loss change?”


The backward-pass rule: upstream gradient times local gradient

Suppose a graph node computes

During backpropagation, imagine you already know how the final loss changes with :

This is called the upstream gradient arriving at . The local operation tells you

The chain rule gives the gradient passed back to :

In words:

A convenient notation in implementations is

Then each node receives and computes . Since the final output is the loss itself,

That is the seed of the backward pass.

Local derivative rules you should know

For scalar values, these common operations are enough to understand a basic network:

Forward operationLocal derivativesBackward implication
Copy the upstream gradient to both inputs
Multiply the upstream gradient by the other input
Multiply by
Multiply by
Prediction error is the upstream signal

Two details often cause errors in interviews and implementation:

  1. The local derivative is evaluated using forward-pass values. This is why frameworks cache selected intermediate results.
  2. A multiply gate sends different gradients to its two inputs. For , the gradient with respect to contains , not .

CS231n Deep Learning for Computer Vision

Read the CS231n notes’ compact scalar example to reinforce the operational view of backpropagation: perform a forward calculation, then propagate the final derivative backward one local operation at a time.

In the section beginning with the complex-expression example, read the scalar graph walkthrough. Follow the forward values for q=x+y and f=qz, then verify why the backward pass produces gradients for all three inputs. Continue into “Intuitive understanding of backpropagation” and read the local-process explanation. Focus on the distinction between a gate’s local derivatives and the incoming gradient from the rest of the graph.


A full worked backward pass through one neuron

Let us calculate every relevant value for a one-feature classifier-like neuron. Use:

The model is:

Forward pass

Compute each intermediate value in order:

The prediction is , below the target , producing a loss of .

Backward pass

Start at the scalar loss. Since is the output,

Now work right to left.

1. Loss to prediction

Because

The negative sign has a meaningful interpretation: increasing slightly would decrease the loss, which makes sense because the target is .

2. Prediction to pre-activation

For sigmoid,

At ,

Apply the chain rule:

It is common to call this quantity the neuron’s error signal or backpropagated signal for the pre-activation.

3. Pre-activation to weight and bias

Recall:

For the weight,

Thus,

For the bias,

so

The parameter gradient is therefore

Notice the structure:

The weight gradient has one additional factor, , because the weight affects through multiplication by the input. This relationship generalizes: for a linear layer, parameter gradients depend on both an error signal and the activation/input entering that parameter.


Gradient descent: turn the gradient into a parameter update

The gradient tells us the direction of greatest local increase in loss. To reduce loss, move in the opposite direction:

Here:

  • is the vector of trainable parameters;
  • is the learning rate;
  • is the gradient of the loss with respect to those parameters.

For our two parameters,

Choose

Then:

Both parameters increase because both gradient components were negative. This is not a separate rule; it follows directly from subtracting a negative number.

Check the resulting forward pass:

The loss decreased:

That is one training step:

  1. make a prediction;
  2. compute a scalar loss;
  3. backpropagate to obtain gradients;
  4. update parameters in the negative-gradient direction.

22.4. Multivariable Calculus

Read the gradient-descent section from Dive into Deep Learning to connect the update rule to the geometric result from the previous lesson: the negative gradient is the locally steepest descent direction.

In “22.4.2. Geometry of Gradients and Gradient Descent,” read the informal optimization procedure, then continue through the negative-gradient derivation. Map its parameter vector \mathbf{w} and step size \epsilon to this lesson’s \boldsymbol{\theta} and \eta.


What changes when a value is used more than once?

So far, each intermediate value had one route to the final output. Real graphs often branch: one value influences the loss through multiple downstream paths.

For example:

The variable appears twice: once directly in the multiplication and once indirectly through . Expanding gives

so

Backpropagation obtains the same result by adding contributions from both paths:

  • direct path :
  • indirect path :

Thus,

The rule is:

Multiply gradients along a single path; add gradients across distinct paths.

In software, this is why gradient accumulation must use addition. A parameter can affect many later operations, and overwriting rather than accumulating its gradient silently produces an incorrect update.


A disciplined manual procedure

For small graphs, use this sequence:

  1. Identify parameters and data.
    Parameters are updated; input data and targets are held fixed for that training step.

  2. Write the computation in stages.
    Introduce meaningful intermediates such as , , and .

  3. Perform a forward pass.
    Evaluate and retain the values needed by local derivatives.

  4. Start the backward pass from the scalar objective.
    Set .

  5. Apply upstream times local derivative at each operation.
    Move in reverse topological order: loss, activation, linear computation, parameters.

  6. Accumulate gradients where the graph branches.

  7. Update parameters simultaneously.
    Use the old parameter values and their corresponding gradients:

A useful interview-level explanation is:

“Backpropagation is reverse-mode automatic differentiation on a computational graph. A forward pass computes and caches intermediate values; a backward pass propagates the scalar loss gradient backward using the chain rule. Gradients multiply through each path and accumulate at branches, producing one derivative per trainable parameter. An optimizer then uses those gradients to update the parameters.”

A final caution: gradient descent guarantees a local first-order decrease only when the learning rate is appropriately small. With too large a learning rate, the linear approximation underlying the step is poor and the loss can rise or diverge. Later lessons will compare optimization methods that alter this basic update rule, but they all begin with the gradients obtained here.


Key takeaways

A computational graph turns a complex model into local operations. The forward pass computes values from inputs to loss; the backward pass computes derivatives from loss back to parameters.

For a chain of dependencies, multiply local derivatives:

For a graph with multiple paths from a variable to the loss, add the gradient contributions from those paths.

Once backpropagation yields the parameter gradient, gradient descent updates parameters as:

In the next lesson, you will move from optimization mechanics to probability, using conditional probability and Bayes’ rule to update beliefs from evidence.

Can't find a good explanation? Sign up and we'll make it for you

Sign up