プリンキピア

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

12.10.3. Yogi12.10.3 Yogi

One of the problems of Adam is that it can fail to converge even in convex settings when the second moment estimate in 𝐬t blows up. As a fix Zaheer et al. (2018) proposed a refined update (and initialization) for 𝐬t. To understand what’s going on, let’s rewrite the Adam update as follows:
英語のヒント
Adamの問題の一つは、𝐬tの2次モーメントの推定値が爆発すると、凸の設定でさえ収束しないことがある点である。その修正として、Zaheer et al. (2018)は、𝐬tの改良した更新(と初期化)を提案した。何が起きているかを理解するため、Adamの更新を次のように書き直そう。
𝐬t←𝐬t−1+(1−β2)(𝐠t2−𝐬t−1).(12.10.5)
Whenever 𝐠t2 has high variance or updates are sparse, 𝐬t might forget past values too quickly. A possible fix for this is to replace 𝐠t2−𝐬t−1 by 𝐠t2⊙sgn⁡(𝐠t2−𝐬t−1). Now the magnitude of the update no longer depends on the amount of deviation. This yields the Yogi updates
英語のヒント
𝐠t2の分散が大きいときや、更新が疎なときはいつでも、𝐬tは過去の値を速く忘れすぎるかもしれない。これに対するありうる修正は、𝐠t2−𝐬t−1を𝐠t2⊙sgn⁡(𝐠t2−𝐬t−1)に置き換えることである。こうすると、更新の大きさはもはや偏差の量に依存しない。これにより、Yogiの更新が得られる。
𝐬t←𝐬t−1+(1−β2)𝐠t2⊙sgn⁡(𝐠t2−𝐬t−1).(12.10.6)
The authors furthermore advise to initialize the momentum on a larger initial batch rather than just initial pointwise estimate. We omit the details since they are not material to the discussion and since even without this convergence remains pretty good.
英語のヒント
著者らはさらに、単に最初の点ごとの推定値ではなく、もっと大きな最初のバッチでモメンタムを初期化することを勧めている。議論に本質的でなく、これがなくても収束はかなりよいままなので、詳細は省く。
def yogi(params, states, hyperparams):
    beta1, beta2, eps = 0.9, 0.999, 1e-3
    for p, (v, s) in zip(params, states):
        with torch.no_grad():
            v[:] = beta1 * v + (1 - beta1) * p.grad
            s[:] = s + (1 - beta2) * torch.sign(
                torch.square(p.grad) - s) * torch.square(p.grad)
            v_bias_corr = v / (1 - beta1 ** hyperparams['t'])
            s_bias_corr = s / (1 - beta2 ** hyperparams['t'])
            p[:] -= hyperparams['lr'] * v_bias_corr / (torch.sqrt(s_bias_corr)
                                                       + eps)
        p.grad.data.zero_()
    hyperparams['t'] += 1

data_iter, feature_dim = d2l.get_data_ch11(batch_size=10)
d2l.train_ch11(yogi, init_adam_states(feature_dim),
               {'lr': 0.01, 't': 1}, data_iter, feature_dim);
loss: 0.243, 0.165 sec/epoch