General model optimization
Train neural networks with a generally effective adaptive optimizer.
Keras · Class
Adam is a Keras optimizer based on adaptive estimates of first- and second-order gradient moments. Configure its learning rate, moment decay, clipping, EMA, and related optimization options before compiling a model.
Adam updates model variables using adaptive estimates of first- and second-order gradient moments. Its behavior can be adjusted with decay rates, numerical-stability epsilon, AMSGrad, weight decay, gradient clipping, EMA, loss scaling, and gradient accumulation options.
keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=False, weight_decay=None, clipnorm=None, clipvalue=None, global_clipnorm=None, use_ema=False, ema_momentum=0.99, ema_overwrite_frequency=None, loss_scale_factor=None, gradient_accumulation_steps=None, name=None, **kwargs)| Name | Type | Description |
|---|---|---|
learning_rate | float, keras.optimizers.schedules.LearningRateSchedule, or callable | Learning rate, schedule, or zero-argument callable; defaults to 0.001. |
beta_1 | float, constant float tensor, or callable | Exponential decay rate for first-moment estimates; defaults to 0.9. |
beta_2 | float, constant float tensor, or callable | Exponential decay rate for second-moment estimates; defaults to 0.999. |
epsilon | float | Small value used by the optimizer for numerical stability; defaults to 1e-07. |
amsgrad | boolean | Whether to use the AMSGrad variant; defaults to False. |
weight_decay | float or None | Weight-decay coefficient, or None to disable it. |
clipnorm | float or None | Per-gradient norm clipping threshold. |
clipvalue | float or None | Per-gradient value clipping threshold. |
global_clipnorm | float or None | Global gradient norm clipping threshold. |
use_ema | boolean | Whether to use exponential moving averages of model weights. |
ema_momentum | float | EMA momentum; defaults to 0.99. |
ema_overwrite_frequency | integer or None | How often to overwrite model weights with EMA values. |
An Adam optimizer instance.
Adam is Keras's optimizer implementation of the Adam algorithm.beta_1 controls decay of the first-moment estimate and defaults to 0.9.beta_2 controls decay of the second-moment estimate and defaults to 0.999.amsgrad is a Boolean switch for using the AMSGrad variant and defaults to False.learning_rate accepts a number, a Keras learning-rate schedule, or a callable.amsgrad=True selects the AMSGrad variant rather than the default Adam update.import keras
model = keras.Sequential([
keras.layers.Dense(10, activation="softmax", input_shape=(4,))
])
model.compile(
optimizer=keras.optimizers.Adam(learning_rate=0.001),
loss="sparse_categorical_crossentropy",
)
Creates Adam with its standard learning rate and uses it to compile a classification model.
Train neural networks with a generally effective adaptive optimizer.
Change step sizes during training.
Limit unusually large updates.
Track smoothed model weights during optimization.
Adam is a stochastic gradient-descent optimizer that adaptively estimates first- and second-order moments of gradients. In Keras, it is configured through the keras.optimizers.Adam class.
Follow-up: How would you vary the learning rate during training? Pass a LearningRateSchedule or a zero-argument callable as learning_rate.
The default learning rate is 0.001; beta_1 and beta_2 default to 0.9 and 0.999 and control exponential decay of first- and second-moment estimates. epsilon defaults to 1e-07 and supports numerical stability.
Follow-up: What does amsgrad change? It enables the AMSGrad variant of Adam when set to True.
Instantiate Adam and pass the resulting optimizer to model.compile, or use the string identifier "adam". Passing the object allows explicit configuration of optimizer parameters.
Follow-up: What happens when the string identifier is used? Keras uses the optimizer's default parameters.
keras.optimizers.AdamWkeras.optimizers.Adamaxkeras.optimizers.Nadamkeras.optimizers.Optimizer