余弦退火+周期性重启+warm-up

一、概念
余弦退货:

学习率预热warm-up:开始训练时,使用一个较小的学习率。

  • 权重初始化是随机的,若一开始使用一个较大的学习率,可能带来模型的不稳定。
二、代码实现
import numpy as np import matplotlib.pyplot as pltdef lr_schedule_cosine(lr_min, lr_max, per_epochs): def compute(epoch): return lr_min + 0.5 * (lr_max - lr_min) * (1 + np.cos(epoch / per_epochs * np.pi)) return compute# 余弦退火策略 draw_x = np.arange(300) draw_y1 = list(map(lr_schedule_cosine(1e-4, 1e-1, 300), draw_x)) draw_y2 = list(map(lr_schedule_cosine(1e-4, 1e-1, 150), draw_x)) plt.subplot(121), plt.plot(draw_x, draw_y1, "b-") plt.subplot(122), plt.plot(draw_x, draw_y2, "r-") plt.show()# 周期性重启 draw_x = np.arange(300) draw_y = list(map(lr_schedule_cosine(1e-4, 1e-1, 100), draw_x % 100)) plt.plot(draw_x, draw_y, "b-") plt.show()# 学习率预热warm-up:初始训练时,使用一个较小的学习率 # 这是一个简单的warm-up策略,你可以进一步优化 lr_warm_up_alpha = 1e-2# 学习率的初始warm-up系数 lr_warm_up_schedule = {# warm-up策略 5: 3e-2,# epoch=5时,系数为0.03 10: 1e-1,# epoch=10时,系数为0.1 15: 3e-1,# epoch=15时,系数为0.3 20: 1# epoch=20时,回归cosine,所以系数为1 } def get_lr(epoch): global lr_warm_up_alpha if epoch in lr_warm_up_schedule: lr_warm_up_alpha = lr_warm_up_schedule[epoch] lr_select = lr_schedule_cosine(1e-4, 1e-1, 100)(epoch % 100) return lr_select * lr_warm_up_alpha draw_x = np.arange(300) draw_y = list(map(get_lr, draw_x)) plt.plot(draw_x, draw_y, "b-") plt.show()

【余弦退火+周期性重启+warm-up】可视化结果:
余弦退火+周期性重启+warm-up
文章图片

    推荐阅读