自动优化算法
自动调整batchsize以适应内存,当然batchsize为相当重要的参数,谨慎修改。
cudnn.benchmark=True自动寻找最适合当前配置的高效算法,以增加运行效率
自动调整学习率
1.自定义根据 epoch 改变学习率
def adjust_learning_rate(optimizer, epoch):
"""Sets the learning rate to the initial LR divided by 5 at 60th, 120th and 160th epochs"""
lr = args.lr * ((0.2 ** int(epoch >= 60)) * (0.2 ** int(epoch >= 120))* (0.2 ** int(epoch >= 160)))
# log to TensorBoard
if args.tensorboard:
log_value('learning_rate', lr, epoch)
for param_group in optimizer.param_groups:
param_group['lr'] = lr
- 余弦退火方法 更多类似函数的详解
scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=args.epochs)
# [学习率图像](https://nbviewer.org/gist/Ernie1/cb489ebc738ce033bae0485ea1da760a)
import torch
import torchvision.models as models
import matplotlib.pyplot as plt
model = models.resnet50()
op = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9)
tmax = 50
epoch = 200
sc = torch.optim.lr_scheduler.CosineAnnealingLR(op, tmax)
lr = []
for i in range(epoch):
lr.append(op.param_groups[0]['lr'])
sc.step()
if (i + 1) % tmax==0:
for _ in range(tmax):
sc.step()
plt.plot(range(epoch), lr)
plt.title('CosineAnnealingLR')
plt.xlabel('epoch')
plt.ylabel('lr')
plt.show()
- warmup方法:再前期使用小的学习率以防止过拟合。
Pytorch:几行代码轻松实现Warm up + Cosine Anneal LR
香侬读 | Transformer中warm-up和LayerNorm的重要性探究