liveBook · Manning

Optimization

As explained in Learn Generative AI with PyTorch

An algorithm that updates model parameters to reduce loss during training.

Optimization updates model parameters to reduce loss during training. In PyTorch, an optimizer uses gradients from the loss to decide how much each parameter should change. Adam extends traditional gradient descent by considering gradients from previous iterations, not only the current one. section 2.3.2 section 10.3

A typical training step connects the optimizer to the model and loss:

  • Choose a learning rate, such as 0.001; tune it empirically or with a validation set. section 2.3.2
  • Compute predictions and a loss, then call loss.backward() to calculate parameter gradients. section 2.3.2
  • Clear old gradients and call optimizer.step() to apply the update. section 2.3.2

Binary image classification

An Adam optimizer trains a binary classifier that distinguishes ankle boots from t-shirts. The model uses BCELoss, processes batches for multiple epochs, and updates parameters after each backward pass. section 2.3.2

lr=0.001
optimizer=torch.optim.Adam(binary_model.parameters(),lr=lr)
loss_fn=nn.BCELoss()

This compact listing shows how PyTorch binds a learning rate, model parameters, Adam, and binary cross-entropy loss. section 2.3.2

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains learning rate selection, gradient computation, gradient clearing, and optimizer steps, without deriving Adam internally.
Examples
Demonstrates Adam with binary image classification and discusses optimizer selection for English-to-French Transformer translation.
What it assumes
Assumes a model, loss function, batches, and basic PyTorch training operations.
What you can do after
Apply it via PyTorch by configuring Adam and stepping through a loss-based training loop.
How it connects
Connects optimizers with gradient descent, loss functions, learning rates, gradients, training loops, and hyperparameter tuning.

Also explained in

Compare how 30 books explain optimization →

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.