第12章最適化アルゴリズム
12.8.2. Implementation from Scratch12.8.2 ゼロからの実装
As before we use the quadratic function to observe the trajectory of RMSProp. Recall that in Section 12.7, when we used Adagrad with a learning rate of 0.4, the variables moved only very slowly in the later stages of the algorithm since the learning rate decreased too quickly. Since is controlled separately this does not happen with RMSProp.
英語のヒント
前と同じように、2次関数を使ってRMSPropの軌跡を観察する。12.7節で学習率0.4のAdagradを使ったとき、学習率が速く下がりすぎたので、アルゴリズムの後の段階では変数がとてもゆっくりとしか動かなかったことを思い出そう。は別に制御されるので、RMSPropではこれは起こらない。
def rmsprop_2d(x1, x2, s1, s2):
g1, g2, eps = 0.2 * x1, 4 * x2, 1e-6
s1 = gamma * s1 + (1 - gamma) * g1 ** 2
s2 = gamma * s2 + (1 - gamma) * g2 ** 2
x1 -= eta / math.sqrt(s1 + eps) * g1
x2 -= eta / math.sqrt(s2 + eps) * g2
return x1, x2, s1, s2
def f_2d(x1, x2):
return 0.1 * x1 ** 2 + 2 * x2 ** 2
eta, gamma = 0.4, 0.9
d2l.show_trace_2d(f_2d, d2l.train_2d(rmsprop_2d))epoch 20, x1: -0.010599, x2: 0.000000Next, we implement RMSProp to be used in a deep network. This is equally straightforward.
英語のヒント
次に、深いネットワークで使うRMSPropを実装する。これも同じくらい素直である。
def init_rmsprop_states(feature_dim):
s_w = torch.zeros((feature_dim, 1))
s_b = torch.zeros(1)
return (s_w, s_b)
def rmsprop(params, states, hyperparams):
gamma, eps = hyperparams['gamma'], 1e-6
for p, s in zip(params, states):
with torch.no_grad():
s[:] = gamma * s + (1 - gamma) * torch.square(p.grad)
p[:] -= hyperparams['lr'] * p.grad / torch.sqrt(s + eps)
p.grad.data.zero_()We set the initial learning rate to 0.01 and the weighting term to 0.9. That is, aggregates on average over the past observations of the square gradient.
英語のヒント
学習率の初期値を0.01に、重みの項を0.9に設定する。つまり、は、2乗した勾配の過去個の観測について平均的に集める。
data_iter, feature_dim = d2l.get_data_ch11(batch_size=10)
d2l.train_ch11(rmsprop, init_rmsprop_states(feature_dim),
{'lr': 0.01, 'gamma': 0.9}, data_iter, feature_dim);loss: 0.245, 0.245 sec/epoch