import grain.autograd; import numir; { auto model = MLP!(float, HostStorage)(3); // auto optim = AdaDelta!(typeof(model))(model); auto optim = make!AdaDelta(model); // 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; auto d = (1.0 - optim.rho) * 0.2 * 0.2; auto diff = cast(float)((0.0 + optim.eps) / (d + optim.eps)) ^^ 0.5; auto n = (1.0 - optim.rho) * diff * diff; assert(approxEqual(w.sliced, [[-optim.lr * diff, -optim.lr, -optim.lr], [-optim.lr, -optim.lr, -optim.lr]].nparray)); auto d_ = optim.den[".fc1.weight"].to!(typeof(w)); auto n_ = optim.num[".fc1.weight"].to!(typeof(w)); assert(approxEqual(d_.sliced, [[d, 0.0, 0.0], [0.0, 0.0, 0.0]].nparray)); assert(approxEqual(n_.sliced[0, 0 .. 1], [n].nparray)); } version (grain_cuda) { auto model = MLP!(float, DeviceStorage)(3); auto optim = AdaDelta!(typeof(model))(model); optim.update(); }