Skip to content
class

NAdam

extendsOptimizer
NAdam

NAdam optimiser — Adam with Nesterov momentum (Dozat, 2016).

Replaces the bias-corrected first-moment term with a Nesterov look-ahead that blends the current gradient gtg_t and the running moment mtm_t. The momentum coefficient μt\mu_t decays each step, and its running product ∏s≤tμs\prod_{s \le t} \mu_s replaces the 1−β1t1 - \beta_1^t bias correction.

Math

μt=β1(1−12⋅0.96tψ),μt+1=β1(1−12⋅0.96(t+1)ψ) \mu_t = \beta_1 \left( 1 - \tfrac{1}{2} \cdot 0.96^{t \psi} \right), \qquad \mu_{t+1} = \beta_1 \left( 1 - \tfrac{1}{2} \cdot 0.96^{(t+1)\psi} \right)

where ψ\psi is momentum_decay. Let Πt=∏s=1tμs\Pi_t = \prod_{s=1}^{t} \mu_s and Πt+1=Πt⋅μt+1\Pi_{t+1} = \Pi_t \cdot \mu_{t+1}.

mt=β1mt−1+(1−β1)gt,vt=β2vt−1+(1−β2)gt2 m_t = \beta_1 m_{t-1} + (1 - \beta_1) g_t, \qquad v_t = \beta_2 v_{t-1} + (1 - \beta_2) g_t^2 dt=vt/(1−β2t)+ϵ d_t = \sqrt{v_t / (1 - \beta_2^t)} + \epsilon θt+1=θt−η (1−μt)1−Πt⋅gtdt−η μt+11−Πt+1⋅mtdt \theta_{t+1} = \theta_t - \eta\, \frac{(1 - \mu_t)}{1 - \Pi_t} \cdot \frac{g_t}{d_t} - \eta\, \frac{\mu_{t+1}}{1 - \Pi_{t+1}} \cdot \frac{m_t}{d_t}

References

Dozat, "Incorporating Nesterov Momentum into Adam" (ICLR Workshop, 2016).

See Also

Adam, AdamW, RAdam.

Attributes

lr_float
Step size η\eta. Default 2e-3 (paper recommendation).
beta1_, beta2_, eps_, weight_decay_float
As in Adam.
momentum_decay_float
Decay constant ψ\psi in the μt\mu_t schedule. Default 4e-3.
m_, v_Storage[]
Per-parameter first- and second-moment buffers.
mu_product_double[]
Running product Πt\Pi_t of all momentum coefficients seen so far, one scalar per parameter.

Notes

mu_product_ is a per-parameter double, not a Storage, because it is a single scalar accumulator rather than a tensor. It is held at F32 precision (F64 for an F64 parameter) — the precision it is checkpointed at, which is also the reference framework's — so a restored optimizer continues bit-identically. tt is the parameter's own update count. Weight decay (if non-zero) is added to gtg_t in the standard L2 form — there is no published "decoupled NAdam".

Constructors

1
ctor

NAdam

void NAdam(int)

Construct a NAdam optimiser bound to params.

Parameters

paramsvector of TensorImpl shared pointers
Parameters to optimise.
lrfloat= None
Step size η\eta. Default 2e-3.
beta1, beta2float= None
EMA decay rates. Defaults 0.9 / 0.999.
epsfloat= None
Denominator stabiliser. Default 1e-8.
weight_decayfloat= None
L2 penalty coefficient. Default 0.
momentum_decayfloat= None
Momentum schedule decay ψ\psi. Default 4e-3.

Static methods

1
static

check_hyperparams

void check_hyperparams(double lr, double beta1, double beta2, double eps, double weight_decay, double momentum_decay)

Check NAdam's hyper-parameters — the reference framework's constructor checks, run by the constructor and set_hyperparams.

Raises

InvalidArgument
If lr, eps, weight_decay or momentum_decay is negative or NaN, or a beta lies outside [0, 1).

Virtual methods

2
virtconst

lr

double lr()

Current learning rate η\eta.

virt

set_lr

void set_lr(double lr)

Set the learning rate.

Methods

5
fn

init_state_slot

void init_state_slot(int i, const int & p)

Allocate zero-initialised mm and vv buffers; initialise mu_product_[i] to 1.0.

fn

set_hyperparams

void set_hyperparams(double lr, double beta1, double beta2, double eps, double weight_decay)

Replace the hyper-parameters between steps; nothing is applied when check_hyperparams rejects them. momentum_decay keeps its constructed value.

fnconst

state_dict_id

→str
int state_dict_id()

Schema identifier for state_dict serialisation.

Returns

str

"nadam_v1".

fn

update_one

void update_one(int i, int & p, const int & g)

Compute per-step μt\mu_t and μt+1\mu_{t+1}, advance mu_product_, then apply the two-term Nesterov update.

Parameters

isize_t
Slot index into m_ / v_ / mu_product_.
pTensorImpl shared pointer
In-place parameter.
gStorage
Gradient gtg_t.