liveBook · Manning

Automatic differentiation

As explained in Deep Learning with PyTorch, Second Edition

A technique for automatically computing derivatives needed for training neural networks.

Training a model requires knowing how a change in each parameter would change the loss. PyTorch’s automatic differentiation engine, autograd, calculates those derivatives without requiring a hand-derived formula for the model. Tensors retain a history of the operations and parent tensors that produced them; autograd uses that history to work back from the loss to the parameters. section 1.5 section 5.5.1

Gradient for a calibration model

Suppose a thermometer-calibration model predicts `w * t_u + b`, with loss `((t_p - t_c)**2).mean()`. Create `params = torch.tensor([1.0, 0.0], requires_grad=True)`, then compute `loss = loss_fn(model(t_u, *params), t_c)` and call `loss.backward()`. Before that call, `params.grad` is `None`; afterward, it holds the loss derivative for each parameter. For the sample data, it is `tensor([4517.2969, 82.6000])`. section 5.5.1

To use those gradients in a training loop:

  • Clear any existing parameter gradients with `params.grad.zero_()` when `params.grad` is not `None`. section 5.5.1
  • Compute predictions and loss, then call `loss.backward()` to populate `params.grad`. section 5.5.1
  • Update parameters using the learning rate and gradient inside `with torch.no_grad():`, then repeat. section 5.5.1
# In[9]:
def training_loop(n_epochs, learning_rate, params, t_u, t_c):
    for epoch in range(1, n_epochs + 1):
        if params.grad is not None:     #1
            params.grad.zero_()

        t_p = model(t_u, *params)
        loss = loss_fn(t_p, t_c)
        loss.backward()

        with torch.no_grad():       #2
            params -= learning_rate * params.grad

        if epoch % 500 == 0:
            print('Epoch %d, Loss %f' % (epoch, float(loss)))

    return params

The complete calibration loop puts gradient clearing, the forward calculation, `backward()`, and the parameter update together. section 5.5.1

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Works through gradient tracking, `backward()`, gradient clearing, and updates under `torch.no_grad()`; stops short of deriving individual operation derivatives.
Examples
Demonstrates thermometer calibration using a linear model, mean squared loss, parameter gradients, and a 5,000-epoch training loop.
What it assumes
Builds on PyTorch tensors and a model and loss function while introducing gradient tracking with `requires_grad=True`.
What you can do after
Apply it via PyTorch autograd to calculate loss gradients and update a two-parameter calibration model without hand-derived derivatives.
How it connects
Connects computation graphs and gradient descent to differentiable models, loss functions, and neural-network training.

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 PyTorch, Second Edition. The section links point to the parts of the book it draws on.