Skip to content
class

Adagrad

extendsOptimizer
Adagrad

Adagrad optimiser — accumulates squared gradients without forgetting.

Maintains a running sum of gt2g_t^2 per parameter and scales the learning rate by its inverse square root. The accumulator grows monotonically so the effective learning rate decays to zero in the limit — well suited to sparse features (NLP, embeddings) but typically too aggressive for dense deep-network weights.

Math

Gt=Gt−1+gt2 G_t = G_{t-1} + g_t^2 ηt=η1+(t−1) λ \eta_t = \frac{\eta}{1 + (t - 1)\,\lambda} θt=θt−1−ηt⋅gtGt+ϵ \theta_t = \theta_{t-1} - \eta_t \cdot \frac{g_t}{\sqrt{G_t} + \epsilon}

where tt is the slot's own step count (1 on its first update) and λ\lambda is lr_decay.

References

Duchi, Hazan & Singer, "Adaptive Subgradient Methods for Online Learning and Stochastic Optimization" (JMLR 12, 2011).

See Also

Adadelta, RMSprop — both add a forgetting factor to address Adagrad's monotonic learning-rate decay.

Parameters

paramsvector of TensorImpl
Parameters to optimise.
lrfloat, default 1e-2
Base step size η\eta.
lr_decayfloat, default 0.0
Per-step decay λ\lambda of the effective step size.
weight_decayfloat, default 0.0
L2 penalty coefficient.
initial_accumulator_valuefloat, default 0.0
Constant used to seed sum_sq_grad_. A non-zero value avoids dividing by a near-zero accumulator on the very first step.
epsfloat, default 1e-10
Added to Gt\sqrt{G_t} (outside the square root).

Attributes

sum_sq_grad_vector of Storage
Per-parameter cumulative sum ∑s≤tgs2\sum_{s \le t} g_s^2.

Notes

Because the accumulator never decays, the effective per-parameter learning rate ηt/(Gt+ϵ)\eta_t / (\sqrt{G_t} + \epsilon) is non-increasing — once a coordinate has seen large gradients its step shrinks for the rest of training. This is precisely what makes Adagrad strong on sparse problems and weak on long dense-network runs.

Constructors

1
ctor

Adagrad

void Adagrad(int)

Construct the optimiser and resize per-slot state vectors.

Parameters

paramsvector of TensorImpl
Parameters to optimise.
lrfloat, default 1e-2
Base step size η\eta.
lr_decayfloat, default 0.0
Per-step decay of the effective step size.
weight_decayfloat, default 0.0
L2 penalty coefficient.
initial_accumulator_valuefloat, default 0.0
Seed value for sum_sq_grad_.
epsfloat, default 1e-10
Numerical stabiliser.

Notes

The order is the reference framework's, not that of the Python wrapper (which keeps eps before initial_accumulator_value for its released signature) — the wrapper passes every argument by keyword.

Static methods

1
static

check_hyperparams

void check_hyperparams(double lr, double lr_decay, double weight_decay, double initial_accumulator_value, double eps)

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

Raises

InvalidArgument
If any argument 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 the squared-gradient accumulator for parameter i.

Parameters

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

Notes

When initial_accumulator_value_ != 0 the buffer is filled with that scalar rather than zeros.

fn

set_hyperparams

void set_hyperparams(double lr, double lr_decay, double weight_decay, double initial_accumulator_value, double eps)

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

Notes

initial_accumulator_value is checked but not applied: it seeds the accumulator once, as the reference framework reads it once at construction, so an edit has no effect there either.

fnconst

state_dict_id

→str
int state_dict_id()

Return the versioned state-dict identifier.

Returns

str

"adagrad_v1".

fn

update_one

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

Apply one Adagrad update to parameter i.

Parameters

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

Notes

Accumulates g2g^2 into sum_sq_grad_[i] then applies p−=ηt⋅g/(G+ϵ)p \mathrel{-}= \eta_t \cdot g / (\sqrt{G} + \epsilon) with the decayed step size ηt=η/(1+(t−1)λ)\eta_t = \eta / (1 + (t - 1)\lambda).