| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import math |
| import warnings |
| from typing import List |
|
|
| from torch.optim import Adam, Optimizer |
| from torch.optim.lr_scheduler import _LRScheduler |
| from torch.optim.lr_scheduler import LambdaLR |
| import math |
| from torch.optim import Optimizer |
|
|
| class PolyLRScheduler(_LRScheduler): |
| def __init__(self, optimizer, initial_lr: float, max_steps: int, exponent: float = 0.9, current_step: int = None): |
| self.optimizer = optimizer |
| self.initial_lr = initial_lr |
| self.max_steps = max_steps |
| self.exponent = exponent |
| self.ctr = 0 |
| super().__init__(optimizer, current_step if current_step is not None else -1) |
|
|
| def step(self, current_step=None): |
| if current_step is None or current_step == -1: |
| current_step = self.ctr |
| self.ctr += 1 |
|
|
| new_lr = self.initial_lr * (1 - current_step / self.max_steps) ** self.exponent |
| for param_group in self.optimizer.param_groups: |
| param_group['lr'] = new_lr |
| |
| def get_polynomial_decay_schedule_with_warmup( |
| optimizer, num_warmup_steps, num_training_steps, lr_end=1e-7, power=1.0, last_epoch=-1 |
| ): |
| """ |
| Create a schedule with a learning rate that decreases as a polynomial decay from the initial lr set in the |
| optimizer to end lr defined by *lr_end*, after a warmup period during which it increases linearly from 0 to the |
| initial lr set in the optimizer. |
| |
| Args: |
| optimizer ([`~torch.optim.Optimizer`]): |
| The optimizer for which to schedule the learning rate. |
| num_warmup_steps (`int`): |
| The number of steps for the warmup phase. |
| num_training_steps (`int`): |
| The total number of training steps. |
| lr_end (`float`, *optional*, defaults to 1e-7): |
| The end LR. |
| power (`float`, *optional*, defaults to 1.0): |
| Power factor. |
| last_epoch (`int`, *optional*, defaults to -1): |
| The index of the last epoch when resuming training. |
| |
| Note: *power* defaults to 1.0 as in the fairseq implementation, which in turn is based on the original BERT |
| implementation at |
| https://github.com/google-research/bert/blob/f39e881b169b9d53bea03d2d341b31707a6c052b/optimization.py#L37 |
| |
| Return: |
| `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. |
| |
| """ |
|
|
| lr_init = optimizer.defaults["lr"] |
| if not (lr_init > lr_end): |
| raise ValueError(f"lr_end ({lr_end}) must be be smaller than initial lr ({lr_init})") |
|
|
| def lr_lambda(current_step: int): |
| if current_step < num_warmup_steps: |
| return float(current_step) / float(max(1, num_warmup_steps)) |
| elif current_step > num_training_steps: |
| return lr_end / lr_init |
| else: |
| lr_range = lr_init - lr_end |
| decay_steps = num_training_steps - num_warmup_steps |
| pct_remaining = 1 - (current_step - num_warmup_steps) / decay_steps |
| decay = lr_range * pct_remaining**power + lr_end |
| return decay / lr_init |
|
|
| return LambdaLR(optimizer, lr_lambda, last_epoch) |
|
|
| def get_cosine_schedule_with_warmup( |
| optimizer: Optimizer, num_warmup_steps: int, num_training_steps: int, num_cycles: float = 0.5, last_epoch: int = -1 |
| ): |
| """ |
| Create a schedule with a learning rate that decreases following the values of the cosine function between the |
| initial lr set in the optimizer to 0, after a warmup period during which it increases linearly between 0 and the |
| initial lr set in the optimizer. |
| |
| Args: |
| optimizer ([`~torch.optim.Optimizer`]): |
| The optimizer for which to schedule the learning rate. |
| num_warmup_steps (`int`): |
| The number of steps for the warmup phase. |
| num_training_steps (`int`): |
| The total number of training steps. |
| num_periods (`float`, *optional*, defaults to 0.5): |
| The number of periods of the cosine function in a schedule (the default is to just decrease from the max |
| value to 0 following a half-cosine). |
| last_epoch (`int`, *optional*, defaults to -1): |
| The index of the last epoch when resuming training. |
| |
| Return: |
| `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. |
| """ |
|
|
| def lr_lambda(current_step): |
| if current_step < num_warmup_steps: |
| return float(current_step) / float(max(1, num_warmup_steps)) |
| progress = float(current_step - num_warmup_steps) / float(max(1, num_training_steps - num_warmup_steps)) |
| return max(0.0, 0.5 * (1.0 + math.cos(math.pi * float(num_cycles) * 2.0 * progress))) |
|
|
| return LambdaLR(optimizer, lr_lambda, last_epoch) |
|
|
| def get_constant_schedule_with_warmup(optimizer, num_warmup_steps: int, last_epoch: int = -1): |
| """ |
| Create a schedule with a constant learning rate preceded by a warmup period during which the learning rate |
| increases linearly between 0 and the initial lr set in the optimizer. |
| |
| Args: |
| optimizer ([`~torch.optim.Optimizer`]): |
| The optimizer for which to schedule the learning rate. |
| num_warmup_steps (`int`): |
| The number of steps for the warmup phase. |
| last_epoch (`int`, *optional*, defaults to -1): |
| The index of the last epoch when resuming training. |
| |
| Return: |
| `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. |
| """ |
|
|
| def lr_lambda(current_step: int): |
| if current_step < num_warmup_steps: |
| return float(current_step) / float(max(1.0, num_warmup_steps)) |
| return 1.0 |
|
|
| return LambdaLR(optimizer, lr_lambda, last_epoch=last_epoch) |
|
|
| class LinearWarmupCosineAnnealingLR(_LRScheduler): |
|
|
| def __init__( |
| self, |
| optimizer: Optimizer, |
| warmup_epochs: int, |
| max_epochs: int, |
| warmup_start_lr: float = 0.0, |
| eta_min: float = 0.0, |
| last_epoch: int = -1, |
| ) -> None: |
| """ |
| Args: |
| optimizer (Optimizer): Wrapped optimizer. |
| warmup_epochs (int): Maximum number of iterations for linear warmup |
| max_epochs (int): Maximum number of iterations |
| warmup_start_lr (float): Learning rate to start the linear warmup. Default: 0. |
| eta_min (float): Minimum learning rate. Default: 0. |
| last_epoch (int): The index of last epoch. Default: -1. |
| """ |
| self.warmup_epochs = warmup_epochs |
| self.max_epochs = max_epochs |
| self.warmup_start_lr = warmup_start_lr |
| self.eta_min = eta_min |
|
|
| super(LinearWarmupCosineAnnealingLR, self).__init__(optimizer, last_epoch) |
|
|
| def get_lr(self) -> List[float]: |
| """ |
| Compute learning rate using chainable form of the scheduler |
| """ |
| if not self._get_lr_called_within_step: |
| warnings.warn( |
| "To get the last learning rate computed by the scheduler, " |
| "please use `get_last_lr()`.", |
| UserWarning, |
| ) |
|
|
| if self.last_epoch == 0: |
| return [self.warmup_start_lr] * len(self.base_lrs) |
| elif self.last_epoch < self.warmup_epochs: |
| return [ |
| group["lr"] + (base_lr - self.warmup_start_lr) / (self.warmup_epochs - 1) |
| for base_lr, group in zip(self.base_lrs, self.optimizer.param_groups) |
| ] |
| elif self.last_epoch == self.warmup_epochs: |
| return self.base_lrs |
| elif (self.last_epoch - 1 - self.max_epochs) % (2 * (self.max_epochs - self.warmup_epochs)) == 0: |
| return [ |
| group["lr"] + (base_lr - self.eta_min) * |
| (1 - math.cos(math.pi / (self.max_epochs - self.warmup_epochs))) / 2 |
| for base_lr, group in zip(self.base_lrs, self.optimizer.param_groups) |
| ] |
|
|
| return [ |
| (1 + math.cos(math.pi * (self.last_epoch - self.warmup_epochs) / (self.max_epochs - self.warmup_epochs))) / |
| ( |
| 1 + |
| math.cos(math.pi * (self.last_epoch - self.warmup_epochs - 1) / (self.max_epochs - self.warmup_epochs)) |
| ) * (group["lr"] - self.eta_min) + self.eta_min for group in self.optimizer.param_groups |
| ] |
|
|
| def _get_closed_form_lr(self) -> List[float]: |
| """ |
| Called when epoch is passed as a param to the `step` function of the scheduler. |
| """ |
| if self.last_epoch < self.warmup_epochs: |
| return [ |
| self.warmup_start_lr + self.last_epoch * (base_lr - self.warmup_start_lr) / (self.warmup_epochs - 1) |
| for base_lr in self.base_lrs |
| ] |
|
|
| return [ |
| self.eta_min + 0.5 * (base_lr - self.eta_min) * |
| (1 + math.cos(math.pi * (self.last_epoch - self.warmup_epochs) / (self.max_epochs - self.warmup_epochs))) |
| for base_lr in self.base_lrs |
| ] |
|
|