Pytorch学习笔记之调整学习率

当网络的学习指标不再提升时,可以torch.optim.lr_scheduler.ReduceLROnPlateau类降低学习率。

class torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.1, patience=10,
 verbose=False, threshold=0.0001, threshold_mode='rel', cooldown=0, min_lr=0, eps=1e-08)

参数说明:

  • optimer (Optimizer) 指的是网络的优化器
  • mode (str),可选择‘min’或者‘max’,min表示当监控量停止下降的时候,学习率将减小,max表示当监控量停止上升的时候,学习率将减小。默认值为‘min’
  • factor (float) 学习率每次降低多少,new_lr = old_lr * factor
  • patience (int) 容忍网路的性能不提升的次数,高于这个次数就降低学习率 。默认值:10
  • verbose (bool) 如果为True,则为每次更新向stdout输出一条消息。 默认值:False
  • threshold (float) 测量新最佳值的阈值,仅关注重大变化。 默认值:1e-4
  • threshold_mode (str) 更新的两种模式 rel 或 abs,‘max’为例,rel:dynamic_threshold = best * ( 1 + threshold ), abs:dynamic_threshold = best + threshold。默认值:rel
  • cooldown (int) 减少lr后恢复正常操作之前要等待的时期数。 默认值:0。
  • min_lr (float or list) 学习率的下限
  • eps (float) 适用于lr的最小衰减。 如果新旧lr之间的差异小于eps,则忽略更新。 默认值:1e-8。

使用示例:

optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=0.7, patience=3, verbose=True,
            threshold=0.001,threshold_mode='rel', cooldown=0, min_lr=1e-6, eps=1e-08)
for epoch in range(num_epoches):
	train(...)
	val_loss = validate(...)
	scheduler.step(val_loss)

你可能感兴趣的:(deep,learn,Pytorch)