Stochastic gradient descent with optional momentum, Nesterov acceleration, and L2 weight decay.
Plain SGD takes the parameter down the negative gradient direction at fixed learning rate :
With momentum coefficient , a per-parameter velocity buffer accumulates a low-pass-filtered gradient (Polyak's heavy ball):
where the dampening coefficient scales the current gradient
when accumulating into . dampening = 0 recovers classical
Polyak momentum.
With Nesterov acceleration (Sutskever et al. 2013 reformulation), the lookahead form is used:
which requires dampening = 0 and momentum > 0.
L2 weight decay with coefficient is applied to the gradient before the momentum update (coupled weight decay, as in the reference framework):
This is the standard L2 regularisation form; AdamW-style decoupled
weight decay is not used here — use AdamW for that variant.
Math
Full update (momentum branch, no Nesterov):
Plain SGD is the special case where the velocity buffer is never allocated. A parameter's first momentum step starts the buffer at its gradient, undamped, as the reference framework does:
The same holds when momentum is switched on part-way through training.
References
Polyak, "Some methods of speeding up the convergence of iteration methods" (1964). Sutskever, Martens, Dahl, Hinton, "On the importance of initialization and momentum in deep learning" (ICML 2013).
See Also
ASGD : averaged SGD with running parameter mean. Adam, AdamW : adaptive moment optimisers.
Attributes
lr_doubleset_lr.momentum_doubledampening_doublenesterov_ is true.weight_ decay_doublenesterov_boolmomentum_ > 0 and dampening_ == 0.moment_std::vector<Storage>init_state_slot on the first step seen by slot only when momentum_ != 0; otherwise it remains empty.Notes
CPU and GPU update paths are dispatched inside update_one based
on the parameter's device. The CPU path is a flat scalar loop over
the raw byte buffer; the GPU path expresses the update as a few MLX
array ops, which compose with the surrounding lazy graph.
Examples
Typical training-loop usage from Python:>>> opt = lucid.optim.SGD(model.parameters(), lr=0.01,
... momentum=0.9, weight_decay=1e-4)
>>> opt.zero_grad()
>>> loss.backward()
>>> opt.step()Constructors
1Construct an SGD optimizer with optional momentum and Nesterov.
Parameters
paramsstd::vector<std::shared_ptr<TensorImpl>>Optimizer.lrdoublemomentumdouble= None0.0). Zero disables momentum.dampeningdouble= None0.0). Must be 0 when nesterov is true.weight_ decaydouble= None0.0).nesterovbool= Nonemomentum > 0 and dampening == 0.Raises
:runtime_errornesterov is requested without positive momentum, or with a non-zero dampening.Static methods
1check_hyperparams
void check_hyperparams(double lr, double momentum, double dampening, double weight_decay, bool nesterov)Check SGD's hyper-parameters against its rules.
The single statement of the rules: the constructor and
set_hyperparams both run it, and the Python wrapper runs it on
every parameter group it is given. They are the reference
framework's constructor checks.
Raises
InvalidArgumentlr, momentum or weight_decay is negative, or nesterov is set without positive momentum and zero dampening.Virtual methods
2Methods
7Make room for one slot's velocity buffer without allocating it.
Parameters
slot_ idxstd::size_tparams_ and moment_.paramconst std::shared_ptr<TensorImpl>&Notes
The buffer is made by the slot's first momentum step, from that step's gradient, so plain SGD allocates no extra memory.
Current momentum coefficient .
set_hyperparams
void set_hyperparams(double lr, double momentum, double dampening, double weight_decay, bool nesterov)Replace the hyper-parameters between steps.
The whole set is checked by check_hyperparams before any of it is
applied, so a rejected call changes nothing. Momentum switched on
mid-run starts each parameter's buffer at its next gradient, as the
first momentum step always does (see update_one).
Checkpoint identifier ("sgd_v1").
Apply the SGD update for one parameter slot.
Dispatches to either the CPU scalar loop or the MLX GPU path based on the parameter's device. Implements the plain-SGD, momentum, Nesterov, and weight-decay branches according to the constructor flags. See the class-level math block for the precise update rule.
Parameters
slot_ idxstd::size_tparams_ and moment_.paramstd::shared_ptr<TensorImpl>&gradconst Storage&Current L2 weight-decay coefficient .