liveBook · Manning

Learning rate

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

The step-size parameter controlling how far an optimization update moves in the direction suggested by the gradient.

The learning rate, written η, controls how far an optimization update moves along the direction indicated by the gradient. For a scalar function, gradient descent uses xcur = xcur − η · f′(xcur). section 1.3.3

The gradient identifies which way to move but not how far. A small η takes conservative steps that may require more iterations; an excessive η can overshoot the minimum, bounce around it, or diverge. section 1.3.3

In PyTorch, the learning rate appears directly in either a manual update or an optimizer configuration:

  • SGD accepts the parameter list and learning rate through torch.optim.SGD([x_param], lr=eta), then applies updates with optimizer.step(). section 1.4
  • Training commonly runs for a fixed number of epochs rather than waiting for zero loss or an imperceptibly small parameter change. section 1.4
  • Treating η as a hyperparameter permits automated search with Optuna, alongside choices such as momentum and network architecture. section 5.4

Scalar minimization

Starting with x = −3.5 and η = 0.1, the manual loop repeatedly computes f(x), calls backward(), updates x, and stops when successive guesses change by at most 10⁻⁵. It reaches tensor([2.0000]). section 1.3.3

GAN configuration

A GAN setup uses separate AdamW optimizers for the generator and discriminator, assigning both η = 0.0001 with betas=(0.0, 0.9). section 9.1.3

Figure 1.7 Three examples of how the learning rate η (also called step size) impacts learning. On the left, η is smaller than necessary. This still reaches the minimum but takes more steps than needed. If we knew the perfect value of η, we could set it just right, to take the smallest number of steps to the minimum value (middle). On the right, η is too big, which causes divergence. We never reach the solution!

Compare undersized, well-chosen, and oversized learning rates: slower convergence, efficient convergence, and divergence. section 1.3.3

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])

Trace a complete manual update loop, including gradient computation, the η-scaled parameter update, gradient clearing, and convergence checking. section 1.3.3

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

How deep it goes
Defines the update rule, explains overshooting and divergence, and maps manual updates to PyTorch optimizers; it stops short of scheduling methods.
Examples
Uses scalar minimization from −3.5 to 2.0, PyTorch SGD, and GAN training with AdamW at η = 0.0001.
What you can do after
Apply it via PyTorch by configuring SGD, AdamW, or manual updates and selecting values through Optuna hyperparameter searches.
How it connects
Connects gradients, derivatives, stochastic gradient descent, optimizers, epochs, momentum, Adam, AdamW, and hyperparameter optimization.

Also explained in

Compare how 16 books explain learning rate →

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.