Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Backpropagation for Hidden Layers

Hello! Welcome to the fifth lesson in our "Neural Networks from Scratch" module.

In the last lesson, we successfully derived the gradients of the loss function for the output layer's weights and biases. We introduced the crucial "error signal" , which represents how the final pre-activation values affect the cost. This was the first step in our backward journey through the network.

Today, we take the next logical step. We will propagate that error signal one layer deeper to find the gradients for a hidden layer. This process is the core of "backpropagation" and reveals the elegant, recursive nature of the algorithm.

This lesson directly addresses the learning outcome: Apply the chain rule to derive the gradient of the loss function with respect to the weights and biases of a hidden layer.

A Quick Recap

From the previous lesson, we established the following for the final layer, :

  • The error signal is .
  • The gradients are and .

Our goal now is to find and . To do this, we first need to find the error signal for the hidden layer, .

Let's look at a diagram to visualize this flow.

Backpropagation in a Multi-Layer Neural Network
This diagram illustrates a neural network with two hidden layers. The black arrows show the forward pass, where data flows from input to output. The purple arrows show the backward pass of backpropagation, where the error gradient is propagated from the output layer back through the hidden layers.

1. The Key Difference: A Neuron's Diverging Influence

When we considered the output layer, each neuron's activation influenced the final cost through a single, direct path.

The situation is different for a neuron in a hidden layer. A single activation in a hidden layer, say , influences the final cost through multiple paths. It contributes to every neuron in the next layer, . Each of these contributions then goes on to affect the final cost.

To correctly calculate how affects the cost, we must sum its influence through all these diverging paths. This is the new and most important concept for this lesson.

The following video from 3Blue1Brown provides an excellent intuition for this idea.

Backpropagation calculus | Deep Learning Chapter 4

Let's return to the 3Blue1Brown video on backpropagation calculus. This segment explains precisely how an activation in a hidden layer affects the cost through multiple pathways in the next layer, requiring us to sum these influences.

Watch from 6:43 to 9:03. Focus on how the derivative of the cost with respect to an activation in layer L-1 is calculated by adding up its influence on each neuron in layer L.

2. Deriving the Hidden Layer Gradients

Let's formalize the intuition from the video. We'll derive the update rules by applying the chain rule, just as before, but with an extra step to account for the diverging paths.

We want to find . For a single weight connecting neuron in layer to neuron in layer , the chain rule gives us:

  • The second term is simple: since , its derivative with respect to is just .
  • The first term, , is the error signal for this hidden neuron, which we'll call .

This means . Our main task is to find a formula for .

Finding the Hidden Error Signal,

We apply the chain rule again to find :

  • The second term is again simple: , the derivative of the hidden layer's activation function.
  • The first term, , is where we must sum the influences. The cost is affected by via all the neurons in the next layer, .

Using the chain rule for a function with multiple intermediate variables, we get:

Let's break this down:

  1. is simply the error signal of neuron in the output layer, , which we already know how to compute from the last lesson.
  2. is the rate of change of the pre-activation of neuron in layer with respect to the activation of neuron in layer . Since , this derivative is simply the connecting weight, .

Substituting these back gives us the crucial relationship:

This means the impact of a hidden activation on the cost is the sum of the errors in the next layer, each weighted by the strength of the connection to it.

Finally, we can write the complete formula for the hidden layer error signal:

Once we have , the gradients for the weights and biases of the hidden layer are found using the same logic as for the output layer:

  • (since )

This derivation is laid out very clearly in the following article.

Error Backpropagation & Gradient Descent for Neural Networks

This article by Dustin Stansbury provides a step-by-step mathematical derivation of the gradients for hidden layer parameters. It's an excellent resource to solidify the equations we've just discussed.

Read the sections 'Gradients for Hidden Layer Parameters' and 'Hidden Layer Biases'. Follow the derivation for the hidden layer weights (Equation 9 through 15) and biases (Equation 16 through 18). Note how Equation 14 defines the hidden layer error signal, δj, which matches our derivation.

3. Vectorization and the Total Derivative

The summation is a clear signal that a matrix multiplication is involved. Your linear algebra background is key to understanding the elegance of the vectorized form.

Let's express our derived quantities as matrices and vectors:

  • : Error matrix for layer , shape (N, n_neurons_L).
  • : Weight matrix for layer , shape (n_neurons_L-1, n_neurons_L).
  • : Pre-activation matrix for layer , shape (N, n_neurons_L-1).
  • : Activation matrix from layer , shape (N, n_neurons_L-2).

The summation term, which represents the backpropagated error before applying the activation derivative, becomes a matrix product:

Let's check the shapes: (N, n_neurons_L) (n_neurons_L, n_neurons_L-1) results in a matrix of shape (N, n_neurons_L-1), which is exactly what we need.

This gives us the fully vectorized equation for the hidden layer error signal:

Here, is the element-wise (Hadamard) product.

Once we have the vectorized , the gradients for the entire layer are:

Notice the beautiful recursive pattern! The formulas for the gradients are identical for every layer; only the calculation of the error signal changes as it propagates backward.

A Deeper Look: The Total Derivative

Given your background in math and data science, it's worth noting that this is more than just a convenient matrix operation. The use of the chain rule in this multi-path context is formally an application of the total derivative, also known as the multivariate or vector chain rule. The simple chain rule, , is a special case.

Because the cost is a function of the activations , which are themselves functions of the hidden activations , we are dealing with a composite function with multiple intermediate variables. The matrix multiplication is the precise implementation of the total derivative required to correctly compute the gradient .

The following article explains this important distinction in detail.

Correcting the Misconception of Backpropagation's Chain Rule

This article from Towards Data Science clarifies a common point of confusion: backpropagation uses the total derivative (vector chain rule), not the simpler single-variable chain rule. This is a more rigorous way to understand why the matrix formulation is not just efficient, but mathematically necessary.

Read 'Section 1 Introduction', the section on the total derivative (which introduces the vector chain rule), and '3.1.2 Hidden layer(s)'. Then, skim '3.2.3 Hidden layer(s)', paying special attention to step 'II', which shows exactly how the total derivative is computed as a matrix product. This connects the theory directly to our vectorized equation.

To see these steps applied in a concrete, numerical example, the following video walks through a similar derivation component by component.

Backpropagation Details Pt. 2: Going bonkers with The Chain Rule

The StatQuest channel provides a very explicit, step-by-step derivation for a simple network. Watching a segment can help solidify how each component of the chain rule is calculated.

You can optionally watch from 1:17 to 5:43. This shows the full chain rule being applied piece by piece to find the derivative with respect to a single hidden weight, w_1. This complements the more conceptual and vectorized explanations.

Conclusion

Fantastic work! You have now derived the complete backpropagation algorithm for a two-layer network. You've navigated the trickiest part of the derivation—understanding how to propagate the error signal from one layer to the one before it.

Key Takeaways:

  • Backpropagation is a recursive algorithm where an error signal () is passed backward through the network.
  • A hidden neuron's activation affects the cost through multiple paths, so its influence is the sum of the errors in the next layer, weighted by the connecting weights.
  • The error signal for a hidden layer is calculated from the error signal of the next layer, : .
  • This summation is efficiently and correctly implemented as a matrix product (), which is an application of the total derivative.
  • Once is known, the gradient calculations for and follow the same pattern as for the output layer.

In our next lesson, we will put all this theory into practice. We'll move from equations to code and implement the full backpropagation algorithm in NumPy to compute gradients for all layers of our network.

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

Sign up