liveBook · Manning

Backpropagation

As explained in Learn Generative AI with PyTorch

A PyTorch call that computes gradients of the loss with respect to model parameters.

Backpropagation computes how the loss changes with respect to model parameters, then supplies gradients that an optimizer uses to update the model. In a typical training loop, it follows the forward pass and loss calculation, making it the step where learning occurs. section 2.2.1 section 2.3.2

A PyTorch training step separates gradient calculation from parameter updates:

  • Run the model on a batch and calculate the loss. section 3.4.1
  • Call zero_grad() before backward() so each batch uses incremental rather than accumulated gradients. section 3.4.1
  • Call loss.backward() to compute gradients for the model parameters. section 2.3.2
  • Call optimizer.step() to adjust parameters using those gradients. section 2.3.2

Training a binary classifier

For a binary clothing classifier, the loop feeds flattened images through the network, computes binary cross-entropy, clears old gradients, backpropagates with loss.backward(), and updates the model with optimizer.step(). The same pattern trains both discriminator and generator networks in a GAN. section 2.3.2 section 3.4.1

Figure 2.1 The steps involved in training a deep learning model

Use the training-process figure to connect the forward pass, loss calculation, backpropagation, and weight update in sequence. section 2.2.1

How Learn Generative AI with PyTorch covers it

How deep it goes
Defines parameter gradients and shows the zero_grad(), backward(), and step() sequence, without deriving the underlying calculus.
Examples
Uses binary clothing classification and GAN training with discriminator and generator update functions.
What it assumes
Assumes familiarity with model parameters, predictions, loss, batches, and optimizers while grounding the sequence in classification and GAN examples.
What you can do after
Apply it via PyTorch by resetting gradients, calling backward(), and stepping an optimizer for each training batch.
How it connects
Connects forward passes, loss functions, gradients, optimizers, model parameters, and neural-network training.

Also explained in

Compare how 17 books explain backpropagation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.