Skip to content
class

RMSprop

extendsOptimizer
RMSprop

RMSprop optimiser — gradient normalisation by EMA of squared magnitude, with optional centring and Polyak-style momentum.

The plain rule replaces Adagrad's monotonic accumulator with an exponential moving average, so the effective learning rate stops shrinking and tracks the current gradient regime instead.

Math

Uncentred:

vt+1=α vt+(1−α) gt2 v_{t+1} = \alpha\,v_t + (1 - \alpha)\,g_t^2 θt+1=θt−η gtvt+1+ϵ \theta_{t+1} = \theta_t - \eta\,\frac{g_t}{\sqrt{v_{t+1}} + \epsilon}

Centred — subtract the squared mean to recover the true variance:

gˉt+1=α gˉt+(1−α) gt \bar g_{t+1} = \alpha\,\bar g_t + (1 - \alpha)\,g_t v~t+1=vt+1−gˉt+12 \tilde v_{t+1} = v_{t+1} - \bar g_{t+1}^2 θt+1=θt−η gtv~t+1+ϵ \theta_{t+1} = \theta_t - \eta\,\frac{g_t}{\sqrt{\tilde v_{t+1}} + \epsilon}

With momentum μ≠0\mu \ne 0 the normalised gradient is folded into a velocity buffer first:

bt+1=μ bt+gtvt+1+ϵ,θt+1=θt−η bt+1 b_{t+1} = \mu\,b_t + \frac{g_t}{\sqrt{v_{t+1}} + \epsilon}, \qquad \theta_{t+1} = \theta_t - \eta\,b_{t+1}

References

Tieleman & Hinton, "Lecture 6.5 — RMSProp" (Coursera 2012).

See Also

Adadelta, Adagrad, Rprop

Parameters

paramsvector of TensorImpl
Parameters to optimise.
lrfloat, default 1e-2
Base step size η\eta.
alphafloat, default 0.99
EMA decay of the squared-gradient running average.
epsfloat, default 1e-8
Numerical stabiliser inside the square root.
weight_decayfloat, default 0.0
L2 penalty coefficient.
momentumfloat, default 0.0
Polyak momentum μ\mu. When zero the momentum buffer is skipped entirely.
centeredbool, default false
Track gˉ\bar g to recover the true variance. Better-conditioned in non-stationary regimes at the cost of one extra buffer.

Attributes

square_avg_vector of Storage
EMA of g2g^2 (always allocated).
grad_avg_vector of Storage
EMA of gg (allocated only when centered == true).
moment_buf_vector of Storage
Momentum buffer (allocated only when momentum != 0).

Notes

State buffers are allocated lazily — code that only ever uses the plain variant does not pay the memory cost of the centred or momentum-augmented variants.

Constructors

1
ctor

RMSprop

void RMSprop(int)

Construct the optimiser and resize per-slot state vectors.

Parameters

paramsvector of TensorImpl
Parameters to optimise.
lrfloat, default 1e-2
Base step size.
alphafloat, default 0.99
EMA decay of the squared-gradient running average.
epsfloat, default 1e-8
Numerical stabiliser.
weight_decayfloat, default 0.0
L2 penalty coefficient.
momentumfloat, default 0.0
Polyak momentum.
centeredbool, default false
Use the centred (true-variance) variant.

Static methods

1
static

check_hyperparams

void check_hyperparams(double lr, double alpha, double eps, double weight_decay, double momentum)

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

Raises

InvalidArgument
If lr, alpha, eps, weight_decay or momentum is negative or NaN.

Virtual methods

2
virtconst

lr

→float
double lr()

Return the current learning rate.

Returns

float

Active value of η\eta.

virt

set_lr

void set_lr(double lr)

Set the active learning rate.

Parameters

lrfloat
New base step size.

Methods

5
fn

init_state_slot

void init_state_slot(int i, const int & p)

Allocate per-slot state buffers for parameter i.

Parameters

iint
Slot index.
Parameter whose shape/dtype/device dictates the buffer layout.

Notes

square_avg_[i] is always allocated. grad_avg_[i] is allocated only when centered_ is true and moment_buf_[i] only when momentum_ is non-zero — see Attributes on RMSprop.

fn

set_hyperparams

void set_hyperparams(double lr, double alpha, double eps, double weight_decay, double momentum, bool centered)

Replace the hyper-parameters between steps; nothing is applied when check_hyperparams rejects them.

Notes

Switching on centered or momentum gives each slot that holds state a zero grad_avg / momentum buffer, as a fresh optimizer built with the feature on would start from. Switching one off keeps the buffer, so switching it on again resumes it, as the reference framework's state does.

fnconst

state_dict_id

→str
int state_dict_id()

Return the versioned state-dict identifier.

Returns

str

"rmsprop_v1".

fn

update_one

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

Apply one RMSprop update to parameter i.

Parameters

iint
Parameter slot index.
Parameter tensor (updated in place).
gStorage
Gradient for p on this step.

Notes

Dispatches to a fused MLX kernel on GPU and a scalar Accelerate loop on CPU; the four variant combinations (plain / centred / momentum / both) share a single branchy path that elides the unused buffer reads.