AdaGrad

Constructors

this
this(Chain target, float lr, float eps)

Members

Functions

initStates
void initStates(string name, V field)
step
void step(string name, V field)

Variables

eps
float eps;
Undocumented in source.
lr
float lr;
Undocumented in source.
memory
StateDict memory;
Undocumented in source.
target
Chain* target;
Undocumented in source.

Examples

import grain.autograd;
import numir;

{
    float lr = 0.1;
    float eps = 1e-8;
    auto model = MLP!(float, HostStorage)(3);
    auto optim = AdaGrad!(typeof(model))(model, lr, eps);
    static assert(isOptimizer!(typeof(optim)));
    model.fc1.weight.data.zero_();
    model.fc1.weight.grad = [[0.2f, 0.0f, 0.0f], [0.0f, 0.0f, 0.0f]].variable
        .data;
    optim.update();
    auto w = model.fc1.weight;
    assert(approxEqual(w.sliced, [[-lr * 0.2 / (0.2 * 0.2 + eps) ^^ 0.5, 0.0,
            0.0], [0.0, 0.0, 0.0]].nparray));
    auto m = optim.memory[".fc1.weight"].to!(typeof(w));
    assert(approxEqual(m.sliced, [[0.2 * 0.2, 0.0, 0.0], [0.0, 0.0, 0.0]]
            .nparray));
}
version (grain_cuda) {
    auto model = MLP!(float, DeviceStorage)(3);
    auto optim = AdaGrad!(typeof(model))(model, 0.1);
    optim.update();
}

Meta