第12章最適化アルゴリズム
12.11.3. Policies12.11.3 方針
While we cannot possibly cover the entire variety of learning rate schedulers, we attempt to give a brief overview of popular policies below. Common choices are polynomial decay and piecewise constant schedules. Beyond that, cosine learning rate schedules have been found to work well empirically on some problems. Lastly, on some problems it is beneficial to warm up the optimizer prior to using large learning rates.
英語のヒント
学習率スケジューラの多種多様なものをすべて扱うことはとてもできないが、以下で、人気のある方針の簡単な概観を与えてみる。よくある選択は、多項式の減衰と区分的に一定なスケジュールである。それ以外に、余弦の学習率スケジュールは、いくつかの問題で実験的にうまくいくことがわかっている。最後に、いくつかの問題では、大きな学習率を使う前にオプティマイザをウォームアップすると役に立つ。
12.11.3.1. Factor Scheduler12.11.3.1 因子スケジューラ
One alternative to a polynomial decay would be a multiplicative one, that is for . To prevent the learning rate from decaying beyond a reasonable lower bound the update equation is often modified to .
英語のヒント
多項式の減衰の代わりの一つは乗法的な減衰、つまりについてとすることだろう。学習率が妥当な下限を超えて減衰しないように、更新式はしばしばに変更される。
class FactorScheduler:
def __init__(self, factor=1, stop_factor_lr=1e-7, base_lr=0.1):
self.factor = factor
self.stop_factor_lr = stop_factor_lr
self.base_lr = base_lr
def __call__(self, num_update):
self.base_lr = max(self.stop_factor_lr, self.base_lr * self.factor)
return self.base_lr
scheduler = FactorScheduler(factor=0.9, stop_factor_lr=1e-2, base_lr=2.0)
d2l.plot(torch.arange(50), [scheduler(t) for t in range(50)])This can also be accomplished by a built-in scheduler in MXNet via the
lr_scheduler.FactorScheduler object. It takes a few more parameters, such as warmup period, warmup mode (linear or constant), the maximum number of desired updates, etc.; Going forward we will use the built-in schedulers as appropriate and only explain their functionality here. As illustrated, it is fairly straightforward to build your own scheduler if needed.英語のヒント
これは、MXNetでは
lr_scheduler.FactorSchedulerオブジェクトによる組み込みのスケジューラでも実現できる。それは、ウォームアップの期間、ウォームアップのモード(線形か一定か)、望む更新の最大回数など、もう少し多くのパラメータをとる。以降は、組み込みのスケジューラを適宜使い、その機能はここでだけ説明する。示したように、必要なら自分のスケジューラを作るのはかなり素直である。12.11.3.2. Multi Factor Scheduler12.11.3.2 多因子スケジューラ
A common strategy for training deep networks is to keep the learning rate piecewise constant and to decrease it by a given amount every so often. That is, given a set of times when to decrease the rate, such as decrease whenever . Assuming that the values are halved at each step we can implement this as follows.
英語のヒント
深いネットワークを訓練するよくある戦略は、学習率を区分的に一定に保ち、ときどき決まった量だけ下げることである。つまり、のような、学習率を下げる時刻の集合が与えられたとき、のときはいつでもと下げる。各ステップで値が半分になるとすると、これは次のように実装できる。
net = net_fn()
trainer = torch.optim.SGD(net.parameters(), lr=0.5)
scheduler = lr_scheduler.MultiStepLR(trainer, milestones=[15, 30], gamma=0.5)
def get_lr(trainer, scheduler):
lr = scheduler.get_last_lr()[0]
trainer.step()
scheduler.step()
return lr
d2l.plot(torch.arange(num_epochs), [get_lr(trainer, scheduler)
for t in range(num_epochs)])The intuition behind this piecewise constant learning rate schedule is that one lets optimization proceed until a stationary point has been reached in terms of the distribution of weight vectors. Then (and only then) do we decrease the rate such as to obtain a higher quality proxy to a good local minimum. The example below shows how this can produce ever slightly better solutions.
英語のヒント
この区分的に一定な学習率のスケジュールの背後にある直観は、重みベクトルの分布の点で停留点に達するまで最適化を進めさせることである。そうしてから(そしてそのときにだけ)、よい局所的な最小値へのもっと質の高い代用品を得るように学習率を下げる。下の例は、これが少しずつよりよい解を生みうることを示している。
train(net, train_iter, test_iter, num_epochs, loss, trainer, device,
scheduler)train loss 0.194, train acc 0.927, test acc 0.86912.11.3.3. Cosine Scheduler12.11.3.3 余弦スケジューラ
A rather perplexing heuristic was proposed by Loshchilov and Hutter (2016). It relies on the observation that we might not want to decrease the learning rate too drastically in the beginning and moreover, that we might want to “refine” the solution in the end using a very small learning rate. This results in a cosine-like schedule with the following functional form for learning rates in the range .
英語のヒント
かなり不可解な経験則が、Loshchilov and Hutter (2016)によって提案された。それは、最初は学習率をあまり急激に下げたくないかもしれないこと、さらに、最後にはとても小さな学習率を使って解を「磨き上げ」たいかもしれないという観察にもとづいている。これにより、の範囲の学習率について、次の関数の形をもつ、余弦のようなスケジュールになる。
(12.11.1)
Here is the initial learning rate, is the target rate at time . Furthermore, for we simply pin the value to without increasing it again. In the following example, we set the max update step .
英語のヒント
ここでは学習率の初期値、は時刻での目標の学習率である。さらに、では、再び増やすことなく、値をに固定する。次の例では、最大の更新ステップをとする。
class CosineScheduler:
def __init__(self, max_update, base_lr=0.01, final_lr=0,
warmup_steps=0, warmup_begin_lr=0):
self.base_lr_orig = base_lr
self.max_update = max_update
self.final_lr = final_lr
self.warmup_steps = warmup_steps
self.warmup_begin_lr = warmup_begin_lr
self.max_steps = self.max_update - self.warmup_steps
def get_warmup_lr(self, epoch):
increase = (self.base_lr_orig - self.warmup_begin_lr) \
* float(epoch) / float(self.warmup_steps)
return self.warmup_begin_lr + increase
def __call__(self, epoch):
if epoch < self.warmup_steps:
return self.get_warmup_lr(epoch)
if epoch <= self.max_update:
self.base_lr = self.final_lr + (
self.base_lr_orig - self.final_lr) * (1 + math.cos(
math.pi * (epoch - self.warmup_steps) / self.max_steps)) / 2
return self.base_lr
scheduler = CosineScheduler(max_update=20, base_lr=0.3, final_lr=0.01)
d2l.plot(torch.arange(num_epochs), [scheduler(t) for t in range(num_epochs)])In the context of computer vision this schedule can lead to improved results. Note, though, that such improvements are not guaranteed (as can be seen below).
英語のヒント
コンピュータビジョンの文脈では、このスケジュールで結果がよくなりうる。しかし、(下で見られるように)そうした改善は保証されないことに注意してほしい。
net = net_fn()
trainer = torch.optim.SGD(net.parameters(), lr=0.3)
train(net, train_iter, test_iter, num_epochs, loss, trainer, device,
scheduler)train loss 0.159, train acc 0.942, test acc 0.90412.11.3.4. Warmup12.11.3.4 ウォームアップ
In some cases initializing the parameters is not sufficient to guarantee a good solution. This is particularly a problem for some advanced network designs that may lead to unstable optimization problems. We could address this by choosing a sufficiently small learning rate to prevent divergence in the beginning. Unfortunately this means that progress is slow. Conversely, a large learning rate initially leads to divergence.
英語のヒント
場合によっては、パラメータを初期化するだけでは、よい解を保証するのに十分でない。これは特に、不安定な最適化問題につながりうるいくつかの高度なネットワークの設計で問題になる。最初の発散を防ぐのに十分小さい学習率を選べば、これに対処できる。残念ながら、これは進み具合が遅いことを意味する。逆に、最初から大きな学習率だと発散につながる。
A rather simple fix for this dilemma is to use a warmup period during which the learning rate increases to its initial maximum and to cool down the rate until the end of the optimization process. For simplicity one typically uses a linear increase for this purpose. This leads to a schedule of the form indicated below.
英語のヒント
このジレンマのかなり単純な直し方は、ウォームアップの期間を使うことである。そのあいだ学習率は最初の最大値まで増え、その後、最適化の過程の終わりまで学習率を冷ましていく。簡単のため、この目的にはふつう線形の増加を使う。これにより、下に示す形のスケジュールになる。
scheduler = CosineScheduler(20, warmup_steps=5, base_lr=0.3, final_lr=0.01)
d2l.plot(torch.arange(num_epochs), [scheduler(t) for t in range(num_epochs)])Note that the network converges better initially (in particular observe the performance during the first 5 epochs).
英語のヒント
ネットワークが最初のうちよりよく収束することに注意してほしい(特に、最初の5エポックのあいだの性能を観察してほしい)。
net = net_fn()
trainer = torch.optim.SGD(net.parameters(), lr=0.3)
train(net, train_iter, test_iter, num_epochs, loss, trainer, device,
scheduler)train loss 0.181, train acc 0.934, test acc 0.901Warmup can be applied to any scheduler (not just cosine). For a more detailed discussion of learning rate schedules and many more experiments see also (Gotmare et al., 2018). In particular they find that a warmup phase limits the amount of divergence of parameters in very deep networks. This makes intuitively sense since we would expect significant divergence due to random initialization in those parts of the network that take the most time to make progress in the beginning.
英語のヒント
ウォームアップは、(余弦に限らず)どのスケジューラにも当てはめられる。学習率のスケジュールについてのもっと詳しい議論と、もっと多くの実験については、(Gotmare et al., 2018)も参照。特に、ウォームアップの段階が、とても深いネットワークでのパラメータの発散の量を制限することが見いだされている。これは直観的に筋が通っている。最初に前進するのに最も時間がかかるネットワークの部分では、ランダムな初期化のために大きな発散が起こると予想されるからである。