プリンキピア

第12章最適化アルゴリズム

12.7.4. Implementation from Scratch12.7.4 ゼロからの実装

Just like the momentum method, Adagrad needs to maintain a state variable of the same shape as the parameters.
英語のヒント
モメンタム法と同じように、Adagradはパラメータと同じ形状の状態変数を保持する必要がある。
def init_adagrad_states(feature_dim):
    s_w = torch.zeros((feature_dim, 1))
    s_b = torch.zeros(1)
    return (s_w, s_b)

def adagrad(params, states, hyperparams):
    eps = 1e-6
    for p, s in zip(params, states):
        with torch.no_grad():
            s[:] += torch.square(p.grad)
            p[:] -= hyperparams['lr'] * p.grad / torch.sqrt(s + eps)
        p.grad.data.zero_()
Compared to the experiment in Section 12.5 we use a larger learning rate to train the model.
英語のヒント
12.5節の実験と比べて、もっと大きな学習率を使ってモデルを訓練する。
data_iter, feature_dim = d2l.get_data_ch11(batch_size=10)
d2l.train_ch11(adagrad, init_adagrad_states(feature_dim),
               {'lr': 0.1}, data_iter, feature_dim);
loss: 0.243, 0.162 sec/epoch