🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEtensorflow

tensorflow Documentation

LOADING ENGINE...

optimizers.Adam()

AI & DATA SCIENCE // optimizers-adam

tf.keras.optimizers.Adam() is an adaptive optimizer that adjusts each parameter's learning rate individually based on estimates of the gradient's first and second moments, and is the most commonly used default optimizer.

Syntax

tf.keras.optimizers.Adam(learning_rate=0.001)

Deep Dive Course

Adam, Adaptive Moment Estimation, maintains a running average of both the gradient itself, the first moment, like momentum, and the squared gradient, the second moment, related to its variance, for every parameter individually, using these to automatically scale each parameter's effective learning rate — parameters with consistently large gradients get smaller effective steps, while parameters with small or infrequent gradients get comparatively larger ones. This per-parameter adaptivity is exactly why Adam tends to converge well with little manual tuning, making it the standard default optimizer choice for most deep learning models.

1Understanding optimizers.Adam()

Adam, Adaptive Moment Estimation, maintains a running average of both the gradient itself, the first moment, like momentum, and the squared gradient, the second moment, related to its variance, for every parameter individually, using these to automatically scale each parameter's effective learning rate — parameters with consistently large gradients get smaller effective steps, while parameters with small or infrequent gradients get comparatively larger ones. This per-parameter adaptivity is exactly why Adam tends to converge well with little manual tuning, making it the standard default optimizer choice for most deep learning models.

💡

Adam's default learning_rate of 0.001 works reasonably well as a starting point for most models — before reaching for a different optimizer entirely, try adjusting Adam's learning rate first, since it's usually the single setting with the biggest effect on training behavior.

editor.html
import tensorflow as tf

optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
print(optimizer.learning_rate.numpy())
localhost:3000

2Practical Example

Here is a real-world application of optimizers.Adam() showing how it is used in production TensorFlow code.

editor.html
import tensorflow as tf
from tensorflow.keras import layers, Sequential

model = Sequential([layers.Dense(1, input_shape=(1,))])
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01), loss='mse')
print(model.optimizer.learning_rate.numpy())
localhost:3000

3Best Practices

Follow these guidelines when working with optimizers.Adam():

1. Start with Adam and its default learning rate as the baseline optimizer for a new model, before experimenting with alternatives

2. Reduce the learning rate if training loss oscillates wildly or diverges to nan, a sign the steps are too large for the current loss landscape

3. Pass an actual Adam() object to compile() rather than the string shortcut 'adam' whenever you need a non-default learning rate

⚠️

Tip: Adam's default learning_rate of 0.001 works reasonably well as a starting point for most models — before reaching for a different optimizer entirely, try adjusting Adam's learning rate first, since it's usually the single setting with the biggest effect on training behavior.

editor.html
import tensorflow as tf

optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
print(optimizer.learning_rate.numpy())
localhost:3000

Examples

Example 01Basic Usage
import tensorflow as tf

optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
print(optimizer.learning_rate.numpy())
Example 02Advanced Example
import tensorflow as tf
from tensorflow.keras import layers, Sequential

model = Sequential([layers.Dense(1, input_shape=(1,))])
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01), loss='mse')
print(model.optimizer.learning_rate.numpy())

Best Practices

  • Start with Adam and its default learning rate as the baseline optimizer for a new model, before experimenting with alternatives
  • Reduce the learning rate if training loss oscillates wildly or diverges to nan, a sign the steps are too large for the current loss landscape
  • Pass an actual Adam() object to compile() rather than the string shortcut 'adam' whenever you need a non-default learning rate

Interview Question

Why does Adam generally require less manual learning-rate tuning than plain SGD?

Hint: Think about what Adam tracks per parameter that plain SGD does not.

Plain SGD applies the exact same global learning rate to every parameter's update, regardless of how large or small, or how consistent or noisy, that parameter's gradient tends to be, which means a single learning rate that works well for one parameter might be far too large or too small for another. Adam instead tracks a running estimate of both the gradient and the squared gradient separately for every individual parameter, and uses those per-parameter statistics to automatically scale each parameter's effective step size — a parameter with a consistently large gradient magnitude gets its effective learning rate scaled down, while one with small or sparse gradients gets scaled up. This automatic, per-parameter adaptation is exactly what makes Adam far more forgiving of a suboptimal global learning rate choice than plain SGD, which has no such per-parameter adjustment mechanism at all.

Exercises

MediumPractice using optimizers.Adam() in a real scenario.
View Solution
import tensorflow as tf

optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
print(optimizer.learning_rate.numpy())

Frequently Asked Questions

Why does Adam generally require less manual learning-rate tuning than plain SGD?

Plain SGD applies the exact same global learning rate to every parameter's update, regardless of how large or small, or how consistent or noisy, that parameter's gradient tends to be, which means a single learning rate that works well for one parameter might be far too large or too small for another. Adam instead tracks a running estimate of both the gradient and the squared gradient separately for every individual parameter, and uses those per-parameter statistics to automatically scale each parameter's effective step size — a parameter with a consistently large gradient magnitude gets its effective learning rate scaled down, while one with small or sparse gradients gets scaled up. This automatic, per-parameter adaptation is exactly what makes Adam far more forgiving of a suboptimal global learning rate choice than plain SGD, which has no such per-parameter adjustment mechanism at all.

Related Functions

optimizers-sgdoptimizers-rmspropmodel-compile