Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 8

Backpropagation: How Neural Networks Learn

Recommended resources

Welcome to the final lesson in our module on core machine learning concepts. In our previous session, we deconstructed the architecture of a feedforward neural network, understanding how it uses layers, neurons, weights, and biases to process information in a "forward pass" and generate a prediction. Now, we'll answer the most critical question: how does the network actually learn from its mistakes?

This lesson will explain backpropagation, the fundamental algorithm that powers the training of virtually all modern neural networks. We will explore this process at a conceptual level, focusing on the intuition behind how a network determines which of its thousands, or even billions, of parameters to adjust to improve its performance. Understanding backpropagation is the final piece of the puzzle in our foundational overview before we move on to the specific architectures that power today's large language models.

The Goal: Learning from Error

Training a neural network is an optimization problem. We start with a network that has randomly initialized weights and biases, meaning its initial predictions are nonsensical. We then show it a training example, let it make a prediction, and compare that prediction to the correct answer. The difference between the prediction and the reality is the error.

This error is quantified by a loss function (or cost function), which we introduced in our lesson on gradient descent. The lower the loss, the better the network's performance. The entire goal of training is to adjust the network's weights and biases to minimize this loss function.

The question backpropagation answers is: given a certain amount of error, how do we assign "blame" to each individual weight and bias in the network? Which parameters should be nudged up, and which should be nudged down, to make the network's next prediction a little bit better?


The Two-Step Process: Forward and Backward

At its heart, the learning process is a two-step cycle that repeats for many thousands of iterations:

  1. Forward Pass: Input data is fed through the network, producing an output (a prediction).
  2. Backward Pass: The error in that prediction is calculated and propagated backward through the network to determine how to update the weights and biases.
This diagram illustrates the two phases of training. The forward pass (steps 1 and 2) sends data through the network to make a prediction. The backward pass (steps 4 and 5) sends error information in the reverse direction to adjust the weights.

Backpropagation is the algorithm that executes this backward pass. The name itself is descriptive: it is the "backward propagation of errors."


An Intuitive Walkthrough

To build a strong mental model of this process, there is no better guide than the 3Blue1Brown video on the topic. It masterfully explains how the network's desired changes at the output layer translate into adjustments in the preceding layers.

Backpropagation, intuitively | Deep Learning Chapter 3

Please watch the following segments from the video "Backpropagation, intuitively". This will give you a powerful visual and conceptual understanding of the algorithm's mechanics.

First, watch the introduction from the beginning to understand the setup and the goal of the algorithm. Next, focus on the section explaining the desired changes for a single training example, from the focus on a single neuron to just before he discusses applying this to the rest of the layer. Pay attention to how the desire to increase or decrease an output neuron's activation creates "incentives" to change the bias, weights, and activations of the previous layer. Finally, watch the core explanation of how these desired changes are propagated backward from one layer to the next. This is the central idea of backpropagation: aggregating the "desired effects" from one layer to form a list of nudges for the layer before it.

The key insight from the video is that backpropagation is a process of distributing responsibility for the error. The output neurons first determine how they need to change. They then communicate this desired change to the neurons in the previous layer, and that layer, in turn, communicates its own desired changes to the layer before it, and so on. Each connection's "blame" is proportional to its contribution during the forward pass—stronger connections (higher weights) that were more active are assigned more responsibility for the error.


An Algorithmic View: The Computational Graph

Your background in computer science provides a powerful lens for understanding backpropagation: as an efficient algorithm operating on a computational graph. Any mathematical expression, including the entire forward pass of a neural network, can be decomposed into a directed acyclic graph (DAG) where nodes represent simple operations (like addition, multiplication, or an activation function) and edges represent the flow of data.

To see this in action, the following video builds the concept of backpropagation from the ground up, reframing it as a reverse pass over a computational graph.

The Most Important Algorithm in Machine Learning

Please watch these two key sections from the video "The Most Important Algorithm in Machine Learning".

First, watch the segment that introduces the computational graph and the forward pass, from "derivative of any arbitrarily complex function" until the heading "Backward Step". This shows how a complex calculation can be visualized as a flow through simple, interconnected nodes. Then, watch the explanation of the backward pass, from "But to perform optimization" until the discussion of neural networks begins. This part is crucial as it demonstrates how the chain rule is applied systematically backward through the graph to calculate the gradient of the final loss with respect to every single parameter.

This perspective demystifies backpropagation significantly. It isn't magic; it's a clever and efficient application of the chain rule from calculus. By breaking the network's computation into a graph of simple, differentiable operations, we can automatically and mechanically calculate the gradient—the "nudge" needed for every parameter. This process, known as automatic differentiation, is exactly what libraries like TensorFlow and PyTorch do under the hood. You define the forward pass (the graph), and the framework automatically derives the backward pass (the gradient calculation).


The Role of Backpropagation in Training

Let's consolidate what we've learned. Backpropagation is not the entire training process, but it is the critical engine for learning. It's the step that calculates the gradients.

A Data Scientist’s Guide to Gradient Descent and Backpropagation Algorithms | NVIDIA Technical Blog

To solidify your understanding, please read a section from the NVIDIA technical blog. It provides a concise, text-based summary of backpropagation's role.

In the article, find the section titled Cost functions, Gradient Descent and Backpropagation in Neural Networks. Start reading from the paragraph that defines backpropagation: read this definition and the subsequent explanation. Notice how it connects the calculation of derivatives (the gradients) to the iterative adjustment of weights and biases to reduce the cost function.

The full training loop for a single batch of data looks like this:

  1. Forward Pass: An input is passed through the network, layer by layer, to compute a final output or prediction.
  2. Loss Calculation: The network's prediction is compared with the true value using a loss function to compute a single number representing the error.
  3. Backward Pass (Backpropagation): The algorithm computes the gradient of the loss function with respect to every weight and bias in the network. It starts at the output and works backward, applying the chain rule at each step.
  4. Parameter Update: The calculated gradients are fed to an optimization algorithm, like Gradient Descent, which updates all weights and biases by taking a small step in the opposite direction of their respective gradients.

This cycle is repeated thousands or millions of times, and with each iteration, the network becomes slightly less wrong.


Conclusion

This lesson completes our journey through the fundamental concepts of machine learning. We started with the high-level paradigms of learning, explored the practicalities of model evaluation and optimization, dissected the architecture of a neural network, and have now uncovered the mechanism by which it learns.

Key Takeaways:

  • Neural networks learn by iteratively minimizing a loss function that measures the error in their predictions.
  • Backpropagation is the algorithm for efficiently computing the gradient of the loss function with respect to all network parameters (weights and biases).
  • It works by propagating the error signal backward from the output layer to the input layer, using the chain rule of calculus at each step to determine each parameter's contribution to the overall error.
  • Thinking of a network as a computational graph makes backpropagation an intuitive algorithm: a forward pass calculates the result, and a backward pass calculates the derivatives.
  • The gradients computed by backpropagation are used by an optimizer (like Gradient Descent) to update the network's parameters in the direction that most effectively reduces the loss.

You now have a complete, conceptual understanding of how a basic neural network is structured and trained. With this foundation, you are well-prepared to move into the specifics of modern AI. In our next module, we will dive into the Transformer Architecture, beginning with the revolutionary self-attention mechanism that enables models like GPT to understand and generate human language.