liveBook · Manning

Optimization

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

An algorithm or object that updates model parameters to reduce a loss function.

An optimizer updates model parameters to reduce a loss. In PyTorch, it receives the parameters to change, uses their gradients, and applies an update controlled by a learning rate. SGD is the simplest example: gradient descent moves parameters toward lower loss, while the optimizer packages the repeated update operation. section 1.4 section 5.1.2

Use an optimizer inside the standard training loop:

  • Create it with the parameters to update, for example torch.optim.SGD(model.parameters(), lr=0.001). section 2.1.3 section 5.1.3
  • For each batch, call optimizer.zero_grad(), compute predictions and loss, call loss.backward(), then call optimizer.step(). section 1.4 section 2.1.3
  • Treat the learning rate as the step-size control: gradients provide the direction, while the optimizer can transform them to handle noisy, exploding, or vanishing gradients. section 5.1.2
  • Replace SGD with another optimizer through the same interface when the task benefits from a different update rule; use a learning-rate scheduler separately when needed. section 5.1.3

Minimizing a one-variable function

A parameter initialized at x = 3.5 can minimize f(x) = (x − 2)² with SGD. The loop clears old gradients, evaluates the function, backpropagates, and steps 60 times; the parameter moves toward 2. section 1.4

Figure 2.2 Diagram of the training loop process. It includes the same three major steps of training a model in PyTorch that we originally described, with matching color-coding. New is the initialization of objects that are reused in every training loop. Solid areas show steps, and dashed arrows show effects.

Use the training-loop diagram to connect optimizer creation with batch updates, loss computation, backpropagation, and parameter changes. section 2.1.3

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

How deep it goes
Explains construction and loop mechanics, then motivates gradient transformations; defers detailed optimizer variants to later treatment.
Examples
Demonstrates quadratic minimization, SGD training of neural networks, AdamW defaults, and FashionMNIST configuration.
What it assumes
Assumes tensors, model parameters, loss functions, gradients, and backpropagation; teaches optimizer usage from a scalar minimization example.
What you can do after
Operate/configure PyTorch optimizers in batch training loops and substitute update algorithms without rewriting the loop.
How it connects
Connects parameters, loss functions, gradients, gradient descent, training loops, and learning-rate schedules.

Also explained in

Compare how 30 books explain optimization →

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.