プリンキピア

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

12.11.2. Schedulers12.11.2 スケジューラ

One way of adjusting the learning rate is to set it explicitly at each step. This is conveniently achieved by the set_learning_rate method. We could adjust it downward after every epoch (or even after every minibatch), e.g., in a dynamic manner in response to how optimization is progressing.
英語のヒント
学習率を調整する一つの方法は、各ステップで明示的に設定することである。これはset_learning_rateメソッドで手軽に実現できる。たとえば、最適化の進み具合に応じて動的に、各エポックの後に(あるいは各ミニバッチの後にさえ)下向きに調整できる。
lr = 0.1
trainer.param_groups[0]["lr"] = lr
print(f'learning rate is now {trainer.param_groups[0]["lr"]:.2f}')
learning rate is now 0.10
More generally we want to define a scheduler. When invoked with the number of updates it returns the appropriate value of the learning rate. Let’s define a simple one that sets the learning rate to η=η0(t+1)−12.
英語のヒント
もっと一般には、スケジューラを定義したい。更新の回数を与えて呼び出すと、学習率の適切な値を返すものである。学習率をη=η0(t+1)−12に設定する簡単なものを定義しよう。
class SquareRootScheduler:
    def __init__(self, lr=0.1):
        self.lr = lr

    def __call__(self, num_update):
        return self.lr * pow(num_update + 1.0, -0.5)
Let’s plot its behavior over a range of values.
英語のヒント
ある範囲の値にわたって、そのふるまいをプロットしよう。
scheduler = SquareRootScheduler(lr=0.1)
d2l.plot(torch.arange(num_epochs), [scheduler(t) for t in range(num_epochs)])
Now let’s see how this plays out for training on Fashion-MNIST. We simply provide the scheduler as an additional argument to the training algorithm.
英語のヒント
では、Fashion-MNISTでの訓練でこれがどうなるかを見よう。訓練のアルゴリズムに、追加の引数としてスケジューラを渡すだけである。
net = net_fn()
trainer = torch.optim.SGD(net.parameters(), lr)
train(net, train_iter, test_iter, num_epochs, loss, trainer, device,
      scheduler)
train loss 0.273, train acc 0.900, test acc 0.886
This worked quite a bit better than previously. Two things stand out: the curve was rather more smooth than previously. Secondly, there was less overfitting. Unfortunately it is not a well-resolved question as to why certain strategies lead to less overfitting in theory. There is some argument that a smaller stepsize will lead to parameters that are closer to zero and thus simpler. However, this does not explain the phenomenon entirely since we do not really stop early but simply reduce the learning rate gently.
英語のヒント
これは前よりかなりうまくいった。二つのことが目立つ。曲線が前よりずっとなめらかだった。第二に、過学習が少なかった。残念ながら、ある戦略がなぜ理論的に過学習を減らすのかは、よく解明された問いではない。ステップの大きさが小さいほど、パラメータがゼロに近く、したがってより単純になるという議論もある。しかし、実際には早期に止めるのではなく、学習率を穏やかに下げるだけなので、これでは現象を完全には説明できない。