You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
3.5 KiB
104 lines
3.5 KiB
6 years ago
|
import logging
|
||
|
import math
|
||
|
import numpy as np
|
||
|
import torch
|
||
|
|
||
|
from .scheduler import Scheduler
|
||
|
|
||
|
|
||
|
logger = logging.getLogger(__name__)
|
||
|
|
||
|
|
||
|
class CosineLRScheduler(Scheduler):
|
||
|
"""
|
||
5 years ago
|
Cosine decay with restarts.
|
||
6 years ago
|
This is described in the paper https://arxiv.org/abs/1608.03983.
|
||
5 years ago
|
|
||
|
Inspiration from
|
||
|
https://github.com/allenai/allennlp/blob/master/allennlp/training/learning_rate_schedulers/cosine.py
|
||
6 years ago
|
"""
|
||
|
|
||
|
def __init__(self,
|
||
|
optimizer: torch.optim.Optimizer,
|
||
|
t_initial: int,
|
||
|
t_mul: float = 1.,
|
||
|
lr_min: float = 0.,
|
||
|
decay_rate: float = 1.,
|
||
6 years ago
|
warmup_t=0,
|
||
6 years ago
|
warmup_lr_init=0,
|
||
6 years ago
|
warmup_prefix=False,
|
||
6 years ago
|
cycle_limit=0,
|
||
6 years ago
|
t_in_epochs=True,
|
||
6 years ago
|
initialize=True) -> None:
|
||
|
super().__init__(optimizer, param_group_field="lr", initialize=initialize)
|
||
|
|
||
|
assert t_initial > 0
|
||
|
assert lr_min >= 0
|
||
|
if t_initial == 1 and t_mul == 1 and decay_rate == 1:
|
||
|
logger.warning("Cosine annealing scheduler will have no effect on the learning "
|
||
|
"rate since t_initial = t_mul = eta_mul = 1.")
|
||
|
self.t_initial = t_initial
|
||
|
self.t_mul = t_mul
|
||
|
self.lr_min = lr_min
|
||
|
self.decay_rate = decay_rate
|
||
6 years ago
|
self.cycle_limit = cycle_limit
|
||
6 years ago
|
self.warmup_t = warmup_t
|
||
6 years ago
|
self.warmup_lr_init = warmup_lr_init
|
||
6 years ago
|
self.warmup_prefix = warmup_prefix
|
||
|
self.t_in_epochs = t_in_epochs
|
||
|
if self.warmup_t:
|
||
|
self.warmup_steps = [(v - warmup_lr_init) / self.warmup_t for v in self.base_values]
|
||
|
super().update_groups(self.warmup_lr_init)
|
||
6 years ago
|
else:
|
||
|
self.warmup_steps = [1 for _ in self.base_values]
|
||
|
|
||
6 years ago
|
def _get_lr(self, t):
|
||
|
if t < self.warmup_t:
|
||
|
lrs = [self.warmup_lr_init + t * s for s in self.warmup_steps]
|
||
6 years ago
|
else:
|
||
6 years ago
|
if self.warmup_prefix:
|
||
|
t = t - self.warmup_t
|
||
|
|
||
6 years ago
|
if self.t_mul != 1:
|
||
6 years ago
|
i = math.floor(math.log(1 - t / self.t_initial * (1 - self.t_mul), self.t_mul))
|
||
6 years ago
|
t_i = self.t_mul ** i * self.t_initial
|
||
6 years ago
|
t_curr = t - (1 - self.t_mul ** i) / (1 - self.t_mul) * self.t_initial
|
||
6 years ago
|
else:
|
||
6 years ago
|
i = t // self.t_initial
|
||
6 years ago
|
t_i = self.t_initial
|
||
6 years ago
|
t_curr = t - (self.t_initial * i)
|
||
6 years ago
|
|
||
|
gamma = self.decay_rate ** i
|
||
|
lr_min = self.lr_min * gamma
|
||
|
lr_max_values = [v * gamma for v in self.base_values]
|
||
|
|
||
6 years ago
|
if self.cycle_limit == 0 or (self.cycle_limit > 0 and i < self.cycle_limit):
|
||
|
lrs = [
|
||
|
lr_min + 0.5 * (lr_max - lr_min) * (1 + math.cos(math.pi * t_curr / t_i)) for lr_max in lr_max_values
|
||
|
]
|
||
|
else:
|
||
|
lrs = [self.lr_min for _ in self.base_values]
|
||
|
|
||
6 years ago
|
return lrs
|
||
6 years ago
|
|
||
|
def get_epoch_values(self, epoch: int):
|
||
|
if self.t_in_epochs:
|
||
|
return self._get_lr(epoch)
|
||
|
else:
|
||
|
return None
|
||
|
|
||
|
def get_update_values(self, num_updates: int):
|
||
|
if not self.t_in_epochs:
|
||
|
return self._get_lr(num_updates)
|
||
|
else:
|
||
|
return None
|
||
6 years ago
|
|
||
|
def get_cycle_length(self, cycles=0):
|
||
|
if not cycles:
|
||
|
cycles = self.cycle_limit
|
||
|
assert cycles > 0
|
||
|
if self.t_mul == 1.0:
|
||
|
return self.t_initial * cycles
|
||
|
else:
|
||
|
return int(math.floor(-self.t_initial * (self.t_mul ** cycles - 1) / (1 - self.t_mul)))
|