Adam optimiser — adaptive moment estimation (Kingma & Ba, 2014).
Maintains exponential moving averages of the gradient and squared gradient for each parameter, then applies a bias-corrected step scaled element-wise by the inverse second-moment .
Weight decay (when non-zero) is added to the gradient before the
moment update — i.e. the L2-regularisation form, NOT the
decoupled AdamW form. Use AdamW for decoupled decay.
Math
Per parameter, with the parameter's own update count
(Optimizer::steps_):
References
Kingma & Ba, "Adam: A Method for Stochastic Optimization" (ICLR 2015). Reddi, Kale & Kumar, "On the Convergence of Adam and Beyond" (ICLR 2018) — AMSGrad variant.
See Also
AdamW : decoupled-weight-decay variant. NAdam : Nesterov-momentum variant. RAdam : variance-rectified variant.
Attributes
lr_float1e-3.beta1_ , beta2_float0.9 and 0.999 from the original paper.eps_float1e-8.weight_ decay_float0 to disable.amsgrad_boolm_ , v_Storage[]scalar_ cache_AdamScalarCacheNotes
counts the updates each parameter has received, as the
reference framework's per-parameter state["step"] does: a
parameter that first receives a gradient late starts its bias
correction at , whatever the other parameters have reached.
AMSGrad (Reddi et al., 2018) is enabled by the amsgrad ctor flag:
the running maximum of replaces in the denominator.
Constructors
1Construct an Adam optimiser bound to params.
Parameters
paramsvector of TensorImpl shared pointersm and one v buffer is allocated per parameter on first step() call.lrfloat= None1e-3.beta1, beta2float= None0.9 and 0.999.epsfloat= None1e-8.weight_ decayfloat= None0.amsgradbool= Nonefalse.Static methods
1check_hyperparams
void check_hyperparams(double lr, double beta1, double beta2, double eps, double weight_decay)Check Adam's hyper-parameters — the reference framework's constructor checks, run by the constructor and set_hyperparams.
Raises
InvalidArgumentlr, eps or weight_decay is negative or NaN, or a beta lies outside [0, 1).Virtual methods
2Current learning rate .
Set the learning rate and invalidate the scalar cache.
Parameters
lrfloatNotes
The 3.4 perf pass folds (and for
AdamW) into AdamScalarCache; mutating here flips
scalar_cache_.valid = false so the cached MLX scalars are
rebuilt on the next step.
Methods
8First-moment EMA decay rate .
Second-moment EMA decay rate .
Denominator stabiliser .
Allocate zero-initialised and buffers for slot_idx.
Parameters
slot_ idxsize_tm_ / v_.paramTensorImpl shared pointerset_hyperparams
void set_hyperparams(double lr, double beta1, double beta2, double eps, double weight_decay, bool amsgrad)Replace the hyper-parameters between steps.
Nothing is applied when check_hyperparams rejects them. The
scalar cache is rebuilt on the next step. Switching amsgrad on
gives each slot that holds state a zero running maximum, as a fresh
optimizer built with it on would start from; switching it off keeps
the maximum, so switching it on again resumes it.
Schema identifier for state_dict serialisation.
Returns
str"adam_v1".
Apply the standard Adam update to a single parameter.
Parameters
slot_ idxsize_tm_ / v_.paramTensorImpl shared pointergradStorageparam shape / dtype / device.Notes
Dispatches to adam_step_gpu (GPU stream) or adam_step_cpu
(CPU stream) with decoupled_wd = false.