liveBook · Manning

Automatic differentiation

As explained in Inside Deep Learning: Math, Algorithms, Models

A mechanism for computing derivatives of model computations automatically, used here to support training of networks with registered parameters.

Training a neural network means changing its parameters to reduce a scalar loss: a number that scores how badly its predictions fit the answers. Derivatives tell us which way to change those parameters. Automatic differentiation lets PyTorch calculate the derivatives instead of requiring a hand-written derivative for each model and loss function. section 1.3 section 2.1

The practical rule is to build the computation with PyTorch operations and mark values that need gradients with `requires_grad=True`. PyTorch tracks the operations leading to a result; calling `.backward()` on a scalar result computes gradients for the marked values. The gradients are available through `.grad`. This computes an update direction—it does not, by itself, change the values. section 1.3 section 1.3.2

Differentiate a small function

For `f(x) = (x − 2)²`, the hand-calculated derivative is `f′(x) = 2x − 4`. At `x = −3.5`, it is `−11`. The PyTorch equivalent is: `def f(x): return torch.pow(x - 2.0, 2)` `x = torch.tensor([-3.5], requires_grad=True)` `value = f(x)` `value.backward()` `print(x.grad) # tensor([-11.])` Before `.backward()`, `x.grad` is `None`. The computed `value` carries a `grad_fn`, indicating that PyTorch tracked how it was made. section 1.3.1 section 1.3.2

A gradient becomes useful when it drives an update. For one value, gradient descent uses `x_new = x − ηf′(x)`, where the learning rate `η` controls step size. Recompute the function and its gradient after each step, then stop when successive guesses change little. A step that is too large can diverge; one that is too small can take many updates. section 1.3.3 section 1.3.1

x = torch.tensor([-3.5], requires_grad=True)

x_cur = x.clone() 
x_prev = x_cur*100                                  #1

epsilon = 1e-5                                      #2

eta = 0.1                                           #3

while torch.linalg.norm(x_cur-x_prev) > epsilon:  
    x_prev = x_cur.clone()                          #4

    value = f(x)                                    #5
    value.backward() 
    x.data -= eta * x.grad


    x.grad.zero_()                                  #6


    x_cur = x.data                                  #7

    print(x_cur)

tensor([2.0000])

The complete quadratic example pairs `value.backward()` with a parameter update, clears `x.grad` after each step, and checks the change between successive guesses against `epsilon`. section 1.3.3

For a network, `Θ` denotes the set of parameters rather than a single `x`. The goal is to reduce losses over input–answer pairs, written `minΘ Σᵢ ℓ(fΘ(xᵢ), yᵢ)`. The gradient `∇Θ` has a value for each parameter. The same loop applies: predict, compute the loss, obtain gradients, and update parameters in the opposite direction. section 2.1.1 section 2.1

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Walks through tracked tensor operations, `.backward()`, and an iterative update; leaves derivative computation itself to PyTorch.
Examples
Uses `f(x) = (x − 2)²` at `x = −3.5`, then iterates toward its minimum in a PyTorch loop.
What it assumes
Assumes Python and some calculus, statistics, linear algebra, and machine learning familiarity, while explaining derivatives through a small example.
What you can do after
Apply it via PyTorch to compute gradients and use them in loss-minimizing parameter updates.
How it connects
Connects derivatives and gradients to loss functions, gradient descent, learning rates, and neural network parameters.

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 Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.