๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #354

PyTorch Optimizers

A practical reference for PyTorch's built-in optimizers โ€” every one already covered conceptually in the Optimization category, with exact syntax and typical default hyperparameters.

Common Optimizers, Quick Reference

OptimizerSyntaxConcept Note
SGDoptim.SGD(params, lr=0.01, momentum=0.9)Stochastic Gradient Descent, Momentum
Adamoptim.Adam(params, lr=0.001, betas=(0.9, 0.999))Adam Optimizer
AdamWoptim.AdamW(params, lr=0.001, weight_decay=0.01)AdamW
RMSpropoptim.RMSprop(params, lr=0.001, alpha=0.99)RMSProp

Code โ€” The Standard Training Step

import torch.optim as optim

optimizer = optim.AdamW(model.parameters(), lr=1e-4, weight_decay=0.01)
scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=50)

for epoch in range(50):
    for x_batch, y_batch in dataloader:
        optimizer.zero_grad()
        loss = loss_fn(model(x_batch), y_batch)
        loss.backward()
        optimizer.step()
    scheduler.step()

Per-Parameter-Group Learning Rates

# Different learning rates for different parts of a model -- e.g. for fine-tuning
optimizer = optim.Adam([
    {"params": model.backbone.parameters(), "lr": 1e-5},    # pretrained backbone: smaller LR
    {"params": model.head.parameters(), "lr": 1e-3}          # new head: larger LR
])

This is exactly the pattern from Fine-Tuning โ€” passing a list of parameter groups, each with its own hyperparameters, instead of a single flat parameter list.

Common Mistakes

  • Passing model.parameters() to the optimizer before freezing some layers with requires_grad=False โ€” the optimizer captures the parameter list at construction time; frozen parameters with requires_grad=False simply won't be updated (since they have no gradient), but it's cleaner to filter them out explicitly.
  • Creating a new optimizer instance every epoch instead of reusing one โ€” this discards the optimizer's internal state (like Adam's momentum estimates), effectively resetting training progress each epoch.

Interview Relevance

Q: "How would you set up an optimizer to fine-tune a pretrained backbone with a small learning rate while training a new classification head with a larger one?" Pass a list of parameter group dictionaries to the optimizer constructor, each specifying its own "params" and "lr" โ€” one group for the backbone's parameters with a small learning rate, another for the new head's parameters with a larger one, letting a single optimizer manage both with different update magnitudes.

Practice Question

Why would recreating the optimizer object fresh every epoch (instead of creating it once before the training loop) hurt Adam's effectiveness specifically?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

PyTorch Optimizers โ€“ FAQs

Quick answers about learning PyTorch Optimizers in Deep Learning.

This free note from CodingNow 2.0 explains PyTorch Optimizers in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including PyTorch Optimizers, is 100% free with no signup required.
With focused practice, most students grasp PyTorch Optimizers in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now