2021-10-28 16:21:23 +00:00
|
|
|
from torch.optim.lr_scheduler import _LRScheduler
|
|
|
|
|
|
|
|
|
|
|
|
class LinearWarmupLR(_LRScheduler):
|
2022-03-25 05:02:39 +00:00
|
|
|
"""Linearly warmup learning rate and then linearly decay.
|
2021-10-28 16:21:23 +00:00
|
|
|
|
2022-03-25 05:02:39 +00:00
|
|
|
Args:
|
|
|
|
optimizer (:class:`torch.optim.Optimizer`): Wrapped optimizer.
|
|
|
|
total_steps (int): Number of total training steps.
|
|
|
|
warmup_steps (int, optional): Number of warmup steps, defaults to 0
|
|
|
|
last_epoch (int, optional): The index of last epoch, defaults to -1. When last_epoch=-1,
|
|
|
|
the schedule is started from the beginning or When last_epoch=-1, sets initial lr as lr.
|
2021-10-28 16:21:23 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, optimizer, total_steps: int, warmup_steps: int = 0, last_epoch: int = -1, **kwargs):
|
|
|
|
self.warmup_steps = warmup_steps
|
|
|
|
self.total_steps = total_steps
|
|
|
|
super().__init__(optimizer, last_epoch=last_epoch)
|
|
|
|
|
|
|
|
def get_lr(self):
|
|
|
|
if self.last_epoch < self.warmup_steps:
|
|
|
|
return [(self.last_epoch + 1) / (self.warmup_steps + 1) * lr for lr in self.base_lrs]
|
|
|
|
else:
|
2022-10-17 09:51:40 +00:00
|
|
|
return [(self.total_steps - self.last_epoch) / (self.total_steps - self.warmup_steps) * lr
|
|
|
|
for lr in self.base_lrs]
|