liveBook · Manning

Learning rate

As explained in Deep Learning with PyTorch, Second Edition

The step-size hyperparameter that controls how much model parameters are changed per update during optimization.

A learning rate controls how far model parameters move during each optimization update. In gradient descent, the update subtracts the gradient multiplied by a small learning rate. This lets parameters with a larger effect on the loss receive larger changes, while keeping each step cautious. section 5.4.1

# In[15]:
def training_loop(n_epochs, learning_rate, params, t_u, t_c):
    for epoch in range(1, n_epochs + 1):
        w, b = params
        t_p = model(t_u, w, b)      #1
        loss = loss_fn(t_p, t_c)
        grad = grad_fn(t_u, t_c, t_p, w, b)      #2

        params = params - learning_rate * grad

        print('Epoch %d, Loss %f' % (epoch, float(loss)))    #3

    return params

The training loop makes the step size visible: `params = params - learning_rate * grad` runs once per epoch. section 5.4.3

When the step is too large—or too small

With a learning rate of `1e-2`, a weight-and-bias fitting loop overshoots repeatedly until its loss becomes `inf`. Lowering the rate to `1e-4` stabilizes training, but updates then become small and progress slows. Changing the learning rate is a form of hyperparameter tuning: it controls training rather than being a parameter fitted by that same optimization. section 5.4.3

The gradient matters as much as the chosen rate. If the weight’s gradient is much larger than the bias’s, one shared rate may be unstable for one parameter yet ineffective for the other. Rescaling the input by `0.1` brings the gradients closer together; the loop can then use `1e-2` without blowing up. section 5.4.4

An optimizer can also adjust learning rates during training. Adam maintains a separate rate for each parameter and updates it automatically. For a SegFormer segmentation model, an AdamW optimizer is configured with `torch.optim.AdamW(model.parameters(), lr=0.00006)`. A useful experiment is to compare learning rates and optimizers by their training dynamics and final performance. section 15.7.2 section 15.10

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Works through update code and contrasting training runs, but does not detail the mechanisms of Adam variants.
Examples
Contrasts divergent and slow weight-and-bias training, then configures AdamW for SegFormer segmentation.
What it assumes
Builds from a model’s loss and parameters to an explicit gradient-descent training loop.
What you can do after
Diagnose/debug with it by reducing oversize updates and checking whether input scaling permits a useful shared learning rate.
How it connects
Connects learning rate to gradients, hyperparameter tuning, input normalization, and adaptive optimizers.

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