Skip to content
class

NoamScheduler

extendsLRScheduler
NoamScheduler

Noam schedule — the Transformer "warm-up then 1/t1/\sqrt{t} decay" rule (Vaswani et al., 2017).

Linearly ramps the lr during the first warmup_steps then decays it as the inverse square root of the step count. Scaled by dmodel−1/2d_\text{model}^{-1/2} so that the peak lr matches the convention used in the original Transformer paper.

Math

ηt=f⋅dmodel−1/2⋅min⁡ ⁣(t−1/2,  t⋅w−3/2) \eta_t = f \cdot d_\text{model}^{-1/2} \cdot \min\!\bigl(t^{-1/2},\; t \cdot w^{-3/2}\bigr)

where ww is warmup_steps, ff is factor, and tt is the step count (compute_lr_at uses epoch directly as tt).

References

Vaswani et al., "Attention Is All You Need" §5.3 (NeurIPS 2017).

Attributes

model_size_int
Transformer hidden width dmodeld_\text{model}.
warmup_steps_int
Number of linear-ramp steps before the inverse-sqrt decay kicks in.
factor_float
Global scale on the schedule.

Notes

Despite the LRScheduler base, Noam is conceptually a step schedule rather than an epoch schedule — call step once per optimiser iteration, not once per epoch.

Constructors

1
ctor

NoamScheduler

void NoamScheduler(Optimizer & opt, int model_size, int warmup_steps, double factor)

Build a Noam (Transformer) schedule.

Parameters

Target optimiser.
model_sizeint
Transformer hidden width dmodeld_\text{model}.
warmup_stepsint
Linear-ramp length before the inverse-sqrt decay starts.
factorfloat, default 1.0
Global scale on the schedule.

Methods

1
fnconst

compute_lr_at

→float
double compute_lr_at(int epoch)

Evaluate the Noam formula at step epoch.

Parameters

epochint
Current step count (Noam treats epoch as a step counter).

Returns

float

Scheduled learning rate.