Model compilation
Training Keras models with adaptive optimization and explicit regularization.
TensorFlow · Class
AdamW is a Keras optimizer that combines Adam’s adaptive moment estimation with weight decay. It supports configurable learning rates, moment coefficients, clipping, EMA, loss scaling, and gradient accumulation.
AdamW combines Adam-style adaptive first- and second-moment estimates with decoupled weight decay. Configure its learning rate and regularization behavior when constructing the optimizer, then pass it to model compilation or gradient-based training.
tf.keras.optimizers.AdamW(learning_rate=0.001, weight_decay=0.004, beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=False, 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='adamw', **kwargs)| Name | Type | Description |
|---|---|---|
learning_rate | float, LearningRateSchedule, or callable | Learning-rate value, schedule, or callable; default 0.001. |
weight_decay | float | Weight-decay value; default 0.004. |
beta_1 | float, constant float tensor, or callable | Exponential decay rate for first-moment estimates; default 0.9. |
beta_2 | float, constant float tensor, or callable | Exponential decay rate for second-moment estimates; default 0.999. |
epsilon | float | Small numerical-stability constant; default 1e-7. |
amsgrad | bool | Whether to use the AMSGrad variant; default False. |
clipnorm, clipvalue, global_clipnorm, use_ema, ema_momentum, ema_overwrite_frequency, loss_scale_factor, gradient_accumu | various | Gradient clipping and optimizer behavior options documented by the constructor. |
An AdamW optimizer instance.
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(1, input_shape=(4,))
])
model.compile(
optimizer=tf.keras.optimizers.AdamW(
learning_rate=1e-3,
weight_decay=4e-3
),
loss="mse"
)
model.fit(tf.random.normal((8, 4)), tf.random.normal((8, 1)), epochs=1)Creates an AdamW optimizer with explicit learning-rate and weight-decay settings, then uses it to compile and train a small Keras model.
Training Keras models with adaptive optimization and explicit regularization.
Changing the learning rate as training progresses.
Controlling update magnitudes or optimizer state behavior.
AdamW is an Adam-based stochastic-gradient optimizer that estimates first- and second-order moments while separately decaying weights. This separates weight decay from the adaptive gradient update.
Follow-up: How can the learning rate vary during training? Pass a LearningRateSchedule or a no-argument callable instead of a fixed scalar.
The constructor accepts a learning rate, weight decay, moment coefficients, epsilon, AMSGrad selection, clipping controls, EMA options, loss scaling, gradient accumulation, and a name. The documented defaults include learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-7, and amsgrad=False.
Follow-up: Which parameter controls second-moment smoothing? beta_2 controls the exponential decay rate for second-moment estimates.
Use AdamW as the optimizer supplied to model.compile, then train with fit, or use it in a custom gradient loop. Its learning rate may be a scalar, schedule, or callable, allowing the update scale to change over time.
Follow-up: What additional optimizer behavior can be enabled? EMA, gradient clipping, loss scaling, and gradient accumulation are available through the constructor options.
tf.keras.optimizers.Adamtf.keras.optimizers.Optimizertf.keras.optimizers.schedules.LearningRateSchedule