Skip to main content

Welcome to a Grasp lesson

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

Lesson 40

Computation Graphs for Complex Functions

Recommended resources

Introduction

In our last lesson, we mastered Stochastic Gradient Descent (SGD), the engine that powers large-scale machine learning optimization. We established that to update a model's parameters, we need to compute the gradient of the loss function. For a simple model like linear regression, this was manageable. However, for a deep neural network—a model with millions of parameters forming a deeply nested function—how can we possibly compute the gradient of the loss with respect to every single parameter? Manually applying the chain rule would be an intractable and error-prone nightmare.

This lesson introduces the elegant solution to this complexity: the computation graph. We will learn how to represent any complex mathematical function, including a neural network, as a structured graph of simple, elementary operations. This representation is not just a theoretical convenience; it is the fundamental data structure that enables deep learning frameworks like PyTorch and TensorFlow to automatically calculate gradients for any model you can imagine. By the end of this lesson, you will be able to visualize a neural network's forward computation as a flow of information through a graph, setting the stage for understanding how gradients flow backward in our next lesson.


From Mathematical Expression to Graph

At its core, a computation graph is a way of breaking down a complex calculation into a series of simple, sequential steps. Think of it as a detailed recipe for a mathematical expression. Each ingredient is an input, and each simple action (add, multiply, etc.) is an operation that produces an intermediate result, until you arrive at the final dish.

Let's start with a simple function: .

We can decompose this into three elementary operations:

  1. Let
  2. Let
  3. Let

We can visualize this sequence as a directed graph where nodes represent variables (inputs, intermediate values, and the final output) and the edges show the dependencies between them. The following blog post by Christopher Olah provides a classic and crystal-clear introduction to this idea.

Calculus on Computational Graphs: Backpropagation - Colah's Blog

This post is a foundational text for understanding backpropagation. We will focus only on the first part, which introduces the concept of a computation graph.

Read the first section, "Computational Graphs." Pay close attention to how the expression is broken down into intermediate variables c and d, and how this forms a graph. Then, follow the example of the forward pass, where numerical values for the inputs are propagated through the graph to compute the final output.

The process of plugging in input values and computing the output by moving forward through the graph is called the forward pass.

This diagram illustrates the computation graph for the function e = (a+b) * (b+1). The nodes represent variables and their computed values during a forward pass (with a=3, b=2), while the edges show the flow of computation. The labeled yellow boxes preview the local derivatives we will use in the next lesson.

The Building Blocks of Complex Functions

The power of this approach lies in its generality. Any function, no matter how complex, can be decomposed into a sequence of simple, pre-defined operations. These elementary operations are the "Lego bricks" of our models.

The video below provides a great walkthrough of this decomposition process.

Computation Graph (C1W2L07)

In this video from the original Deep Learning specialization, Andrew Ng explains how to conceptualize a function as a computation graph.

Watch from the beginning, where he introduces the idea of forward and backward propagation. Then, follow his step-by-step construction of a graph for a new example function, J(a,b,c). Finally, note his point about why this is useful: we want to optimize a distinguished output variable—for us, that's the loss function.

Each of these elementary operations can be thought of as a "box" or a simple function with its own inputs and outputs. A computation graph is simply a network of these boxes connected together. For neural networks, these building blocks include:

  • Arithmetic operations: addition, multiplication, subtraction.
  • Matrix operations: dot products, matrix-vector multiplication.
  • Activation functions: Sigmoid (), ReLU, Tanh.
  • Loss functions: Mean Squared Error, Cross-Entropy.

The following Stanford lecture notes formalize this idea and show how these simple blocks combine.

[PDF] Machine learning: backpropagation - CS221 Stanford

These notes provide a more formal computer science perspective on computation graphs.

First, read the formal "Definition: computation graph" and the "Upshot" to understand its purpose (read the definition). Then, review the conceptual model of 'Functions as boxes' and the gallery of 'Basic building blocks' to see the elementary operations that form the basis of neural networks.


From a Single Neuron to a Neural Network

Now, let's apply this to our main subject: neural networks. A neural network is nothing more than a very large composite function, making it perfectly suited for representation as a computation graph.

Consider a single neuron in a network layer. Its output, the activation , is typically calculated as:

This can be broken down into:

  1. A dot product:
  2. An addition (the bias):
  3. An activation function:

When we stack layers of these neurons, we are just composing more and more of these operations, creating a deep and complex graph. The inputs to the graph are the data features () and all the model's parameters (the weights and biases ). The final node in the graph is the loss function, which measures how wrong the model's prediction is.

A simple two-layer neural network. The forward pass, which computes the prediction ŷ, is shown by the blue arrows. This path from inputs (x₁, x₂) to the cost function (J) represents the computation graph. The red arrows show the backward pass for computing gradients, which we will cover next.

The forward pass in a neural network is the process of taking an input (like a vector of gene expression levels) and passing it through the entire graph, layer by layer, to compute the final loss. This is exactly what happens when you call model.forward(x) or model.predict(x) in a deep learning library.

The following resources build on our simple examples to show what the graph for a complete, albeit small, neural network looks like.

Neural Networks 6 Computation Graphs and Backward Differentiation

This video provides another concise and clear construction of a computation graph, culminating in an example for a full neural network.

Watch the definition (what a computation graph is) and the simple construction example (L(a,b,c)). Then, observe the final graph shown for a two-layer neural network. You don't need to follow every detail of the equations, but appreciate how the structure we've learned scales up.

To see this construction again in text form, let's return to the Stanford notes.

[PDF] Machine learning: backpropagation - CS221 Stanford

Let's continue with the Stanford notes to see how the basic blocks compose into real ML models.

First, see how blocks combine in "Function composition" to form a more complex function (simple composition). Then, look at the graph for a real machine learning model, "Linear classification with hinge loss" (hinge loss graph). Finally, observe the complete graph for a "Two-layer neural network" (network graph).


Conclusion

In this lesson, we demystified the structure of complex functions like neural networks by representing them as computation graphs. This representation provides a clear, systematic way to think about the flow of information during a model's prediction phase, known as the forward pass. You now understand that deep learning frameworks aren't magic; they rely on this explicit graph structure to organize computation. For your own work in computational biology, you can now envision massive models like AlphaFold as enormous computation graphs, where protein sequences are the input and the predicted 3D structure is the output of a very deep forward pass.

Key Takeaways:

  • A computation graph decomposes a complex function into a directed acyclic graph (DAG) of elementary operations.
  • Nodes represent variables (inputs, intermediate values, parameters), and edges represent the flow of data.
  • The forward pass is the process of evaluating this graph from inputs to the final output node (the loss function) by applying each operation in sequence.
  • Any neural network, no matter how complex, can be represented as a computation graph. This is the core abstraction that enables automatic differentiation in frameworks like PyTorch and TensorFlow.

We have successfully built the structure and performed the forward pass. The critical question remains: how do we compute the gradients? The answer lies in traversing this graph in the opposite direction. In our next lesson, we will introduce the multivariable chain rule and see how it gives rise to the backpropagation algorithm, an elegant method for calculating all parameter gradients by flowing information backward through the very graph we learned to build today.