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

Adam Optimizer

Adam (Adaptive Moment Estimation) combines momentum's smoothed gradient direction with RMSProp's per-parameter adaptive scaling, plus a bias-correction step neither of those alone includes. It's the most widely used optimizer in deep learning today, and a very reasonable default for most new projects.

Formula

\[ m_t = \beta_1 m_{t-1}+(1-\beta_1)g_t \qquad \text{(first moment โ€” like momentum)} \] \[ v_t = \beta_2 v_{t-1}+(1-\beta_2)g_t^2 \qquad \text{(second moment โ€” like RMSProp)} \] \[ \hat m_t = \frac{m_t}{1-\beta_1^t}, \qquad \hat v_t = \frac{v_t}{1-\beta_2^t} \qquad \text{(bias correction)} \] \[ w_{t+1} = w_t - \eta\frac{\hat m_t}{\sqrt{\hat v_t}+\epsilon} \]

Default hyperparameters (rarely need changing): \(\beta_1=0.9\), \(\beta_2=0.999\), \(\epsilon=10^{-8}\).

Why Bias Correction Is Needed

Both \(m_0\) and \(v_0\) start at exactly 0. In the very early steps of training, this initialization biases \(m_t\) and \(v_t\) toward zero โ€” they haven't had enough steps to "warm up" to their true running-average values yet. Dividing by \((1-\beta_1^t)\) and \((1-\beta_2^t)\) โ€” both very small for small \(t\), close to 1 for large \(t\) โ€” exactly corrects for this early-step bias, boosting the estimates back to their proper scale early in training and having almost no effect once \(t\) is reasonably large.

Numerical Example โ€” First Two Steps

With \(\beta_1=0.9\), \(\beta_2=0.999\), \(g_1=2\): \(m_1 = 0.9(0)+0.1(2)=0.2\), \(v_1=0.999(0)+0.001(4)=0.004\). Bias-corrected: \(\hat m_1 = \frac{0.2}{1-0.9}=2.0\), \(\hat v_1=\frac{0.004}{1-0.999}=4.0\) โ€” notice the correction restores \(\hat m_1\) and \(\hat v_1\) to values matching the raw gradient's own scale (\(g_1=2\), \(g_1^2=4\)), exactly compensating for the zero-initialization bias at this very first step.

Code

import torch.optim as optim

optimizer = optim.Adam(model.parameters(), lr=0.001, betas=(0.9, 0.999), eps=1e-8)

# A full training step, exactly as with any other optimizer
loss.backward()
optimizer.step()
optimizer.zero_grad()

Why Adam Became the Default

  • Combines the benefits of momentum (smoothed direction, faster convergence in consistent directions) and RMSProp (adaptive per-parameter scaling, robust to varying gradient magnitudes across parameters) in one optimizer.
  • Works reasonably well "out of the box" across a very wide range of architectures and tasks, with minimal hyperparameter tuning required โ€” a major practical convenience.
  • Handles sparse gradients well, similar to AdaGrad/RMSProp's original motivation.

Common Mistakes

  • Assuming Adam always outperforms plain SGD with momentum โ€” for some tasks (notably, some computer vision benchmarks), well-tuned SGD with momentum and a good learning rate schedule can generalize slightly better than Adam, though Adam usually converges faster and needs less tuning to get a solid result.
  • Forgetting Adam's interaction with L2 regularization is subtly broken โ€” this specific issue, and its fix, is the entire subject of the next note, AdamW.

Interview Relevance

Q: "What two ideas does Adam combine, and why does it also need bias correction?" It combines momentum (an exponentially weighted average of gradients, the "first moment") and RMSProp-style per-parameter adaptive scaling (an exponentially weighted average of squared gradients, the "second moment"). Bias correction is needed because both moving averages are initialized at zero, which biases their early estimates toward zero until enough steps have accumulated โ€” the correction terms compensate for exactly this early-training bias.

Practice Question

Why does the bias-correction term \(\frac{1}{1-\beta_1^t}\) have almost no effect once \(t\) becomes large (e.g. \(t=1000\)), compared to its large effect at \(t=1\)?

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

Adam Optimizer โ€“ FAQs

Quick answers about learning Adam Optimizer in Deep Learning.

This free note from CodingNow 2.0 explains Adam Optimizer 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 Adam Optimizer, is 100% free with no signup required.
With focused practice, most students grasp Adam Optimizer 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