General neural-network optimization
Training Keras models with adaptive first- and second-moment optimization plus Nesterov momentum.
TensorFlow · Class
Nadam is a Keras optimizer that combines Adam-style adaptive moment estimates with Nesterov momentum. It also supports clipping, weight decay, EMA, loss scaling, and gradient accumulation.
Nadam updates parameters with Adam's adaptive first- and second-moment estimates while adding Nesterov momentum. Configure its learning rate and moment-decay factors, with optional weight decay, gradient clipping, EMA, loss scaling, and gradient accumulation.
tf.keras.optimizers.Nadam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, 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="nadam", **kwargs)| Name | Type | Description |
|---|---|---|
learning_rate | float, learning-rate schedule, or callable | Learning rate; defaults to 0.001. |
beta_1 | float, constant float tensor, or zero-argument callable | Exponential decay rate for first-moment estimates; defaults to 0.9. |
beta_2 | float, constant float tensor, or zero-argument callable | Exponential decay rate for second-moment estimates; defaults to 0.999. |
epsilon | float | Small value used to avoid division by zero; defaults to 1e-7. |
weight_decay | float or None | Weight-decay coefficient; defaults to None. |
clipnorm | float or None | Maximum norm for each gradient; defaults to None. |
clipvalue | float or None | Maximum absolute value for each gradient; defaults to None. |
global_clipnorm | float or None | Maximum global norm for gradients; defaults to None. |
use_ema | bool | Whether to apply exponential moving averages to model weights; defaults to False. |
ema_momentum | float | EMA momentum; defaults to 0.99 and is used only with EMA enabled. |
ema_overwrite_frequency | int or None | How often to overwrite weights with EMA weights; defaults to None. |
loss_scale_factor | float or None | Loss-scale factor for mixed-precision training; defaults to None. |
A Nadam optimizer instance.
tf.keras.optimizers.Nadam is the Keras optimizer class for the Nadam 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.use_ema=True enables exponential moving averaging of model weights.gradient_accumulation_steps averages gradients across multiple steps before applying updates.ema_momentum setting has an effect only when exponential moving averages are enabled.import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(1, input_shape=(4,))
])
model.compile(optimizer=tf.keras.optimizers.Nadam(learning_rate=1e-3), loss="mse")
model.fit(tf.random.normal((32, 4)), tf.random.normal((32, 1)), epochs=1, verbose=0)Creates a Nadam optimizer with a custom learning rate and uses it to train a small Keras model.
Training Keras models with adaptive first- and second-moment optimization plus Nesterov momentum.
Keeping gradient magnitudes within defined bounds during unstable training.
Approximating larger effective batches when memory limits prevent larger batches.
Smoothing model weights through exponential moving averages.
Nadam is an Adam optimizer variant that uses Nesterov momentum. Its defaults include a learning rate of 0.001, beta_1 of 0.9, beta_2 of 0.999, and epsilon of 1e-7.
Follow-up: How can Nadam reduce gradient instability? Configure clipnorm, clipvalue, or global_clipnorm to constrain gradients before updates.
Set use_ema=True to maintain an exponential moving average of model weights. The optimizer can periodically overwrite the model weights with the moving average, and ema_momentum controls the averaging rate.
Follow-up: When is ema_momentum ignored? It is only relevant when use_ema is true.
When gradient_accumulation_steps is an integer, gradients are averaged across that many steps before model and optimizer variables are updated. This is useful when an effective larger batch is desired without storing the full batch.
Follow-up: What trade-off follows? Parameter updates occur less frequently, so optimization behavior differs from updating after every batch.
tf.keras.optimizers.Optimizertf.keras.optimizers.Adamtf.keras.optimizers.Adamax