ディープラーニングでは学習率を徐々に減衰させていくと、いい学習ができる。イメージとしては、最初の方は大ざっぱに調節していき、最後に微調整していくような感じ。ただ最初の方は減衰させずにやりたいとかいろいろカスタマイズしたかったので実装した。
import math class lr_decay: def __init__(self, decay_start, decay_stop, lr_start, lr_end): self.decay_rate = abs((math.log10(lr_start) - math.log10(lr_end))/(decay_start - decay_stop)) self.decay_start = decay_start self.decay_stop = decay_stop self.lr_start = lr_start self.lr_end = lr_end def get_lr(self, idx): if idx < self.decay_start: return self.lr_start if idx > self.decay_stop: return self.lr_end return 10**(math.log10(self.lr_start) - (idx - self.decay_start)*self.decay_rate) epoch = 100 lr = lr_decay(decay_start=int(epoch*0.25), decay_stop=int(epoch*0.75), lr_start=1e-4, lr_end=1e-5)
- decay_start:減衰開始のエポック数
- decay_stop:減衰終了のエポック数
- lr_start:開始の学習率
- lr_end:終了の学習率
減衰の仕方は以下のような感じ。
import matplotlib.pyplot as plt lrs = [lr.get_lr(i) for i in range(epoch)] plt.plot(lrs) plt.show()