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(); }
http://jmlr.org/papers/v12/duchi11a.html