-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
92 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,54 @@ | ||
""" | ||
Learnin rate scheduler with linear warmup phase and cosine decay. | ||
""" | ||
from typing import List | ||
|
||
import numpy as np | ||
from torch import optim | ||
|
||
|
||
class CosineWarmupScheduler(optim.lr_scheduler._LRScheduler): | ||
""" | ||
Cosine learning rate scheduler with linear warmup. | ||
Args: | ||
optim (optim): learning rate scheduler | ||
""" | ||
|
||
def __init__(self, optimizer: optim.Optimizer, warmup: int, max_iters: int): | ||
""" | ||
Cosine learning rate scheduler with linear warmup. | ||
Args: | ||
optimizer (optim.Optimizer): _description_ | ||
warmup (int): number of warmup iterations | ||
max_iters (int): maximum number of iterations | ||
""" | ||
self.warmup = warmup | ||
self.max_num_iters = max_iters | ||
super().__init__(optimizer) | ||
|
||
def get_lr(self) -> List[float]: | ||
lr_factor = self.get_lr_factor(epoch=self.last_epoch) | ||
""" | ||
Get the learning rate. | ||
Returns: | ||
List[float]: List of learning rates. | ||
""" | ||
lr_factor = self.get_lr_factor(iteration=self.last_epoch) | ||
return [base_lr * lr_factor for base_lr in self.base_lrs] | ||
|
||
def get_lr_factor(self, epoch: int) -> float: | ||
def get_lr_factor(self, iteration: int) -> float: | ||
""" | ||
Get the learning rate factor for the given epoch. | ||
Args: | ||
epoch (int): epoch number | ||
lr_factor = 0.5 * (1 + np.cos(np.pi * epoch / self.max_num_iters)) | ||
if epoch <= self.warmup: | ||
lr_factor *= epoch * 1.0 / self.warmup | ||
Returns: | ||
float: learning rate factor | ||
""" | ||
lr_factor = 0.5 * (1 + np.cos(np.pi * iteration / self.max_num_iters)) | ||
if iteration <= self.warmup: | ||
lr_factor *= iteration * 1.0 / self.warmup | ||
return lr_factor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters