liveBook · Manning

Automatic differentiation

As explained in Deep Learning with Python, Third Edition

A mechanism that programmatically computes exact derivatives of recorded operations by applying the chain rule to a computation graph.

Training a neural network requires the gradient of its loss with respect to its weights. That loss may pass through many tensor operations, so writing a separate derivative calculation for every model is impractical. Automatic differentiation makes the calculation programmable: it represents the operations as a computation graph and applies the chain rule to obtain gradients. section 2.4.4 section 3.1

Start with two functions

Suppose x1 = g(x) and y = f(x1). The derivative of y with respect to x is grad(y, x1) × grad(x1, x). Add more intermediate functions, and the result becomes a longer chain of local derivatives. This is the calculation backpropagation applies to a neural network. section 2.4.4

def fghj(x):
    x1 = j(x)
    x2 = h(x1)
    x3 = g(x2)
    y = f(x3)
    return y

grad(y, x) == grad(y, x3) * grad(x3, x2) * grad(x2, x1) * grad(x1, x)

The function composition and its gradient expression show how each intermediate result contributes one factor to the chain. section 2.4.4

A computation graph records operations as a directed acyclic graph rather than leaving them only as lines of code. A model’s forward computation can include matrix multiplication, addition, ReLU, softmax, and a loss function. The graph provides an explicit structure for calculating gradients through that sequence. section 2.4.4

loss_value = loss(
    y_true,
    softmax(matmul(relu(matmul(inputs, W1) + b1), W2) + b2),
)

This nested loss expression shows why automatic differentiation matters in practice: the gradient must pass through several composed tensor operations. section 2.4.4

In practice, use a deep learning framework rather than implementing gradient logic from scratch. JAX, TensorFlow, PyTorch, and Keras provide automatic differentiation alongside tensor computation and distributed computation. The key condition remains that the function being differentiated is composed of differentiable operations. section 3.1 section 2.4.4

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains the chain rule through function composition and computation graphs, then connects it to forward and backward passes.
Examples
Uses composed functions and a two-layer model’s loss expression to show the operations whose gradients must be calculated.
What it assumes
Introduces the needed chain-rule identity while assuming familiarity with functions and basic neural-network tensor operations.
What you can do after
Evaluate/understand how frameworks calculate gradients through differentiable model operations instead of requiring handwritten gradient logic.
How it connects
Connects the chain rule, computation graphs, tensor operations, backpropagation, and deep learning frameworks.

Also explained in

Compare how 6 books explain automatic differentiation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.