|
3 | 3 |
|
4 | 4 | if 'TORCH' in get_backends(): |
5 | 5 | from torch.optim.lr_scheduler import ReduceLROnPlateau, \ |
6 | | - CosineAnnealingLR, ExponentialLR, LambdaLR, MultiStepLR, StepLR |
| 6 | + CosineAnnealingLR, ExponentialLR, LambdaLR, MultiStepLR, StepLR, \ |
| 7 | + OneCycleLR |
7 | 8 |
|
8 | 9 | class DefaultPyTorchSchedulerCallback(AbstractCallback): |
9 | 10 | """ |
@@ -47,6 +48,125 @@ def at_epoch_end(self, trainer, **kwargs): |
47 | 48 | self.scheduler.step(epoch=kwargs.get("curr_epoch", None)) |
48 | 49 | return {} |
49 | 50 |
|
| 51 | + class OneCycleLRCallback(DefaultPyTorchSchedulerCallback): |
| 52 | + """ |
| 53 | + Wraps PyTorch's `OneCycleLR` Scheduler as Callback |
| 54 | +
|
| 55 | + """ |
| 56 | + |
| 57 | + def __init__( |
| 58 | + self, |
| 59 | + optimizer, |
| 60 | + max_lr, |
| 61 | + total_steps=None, |
| 62 | + epochs=None, |
| 63 | + steps_per_epoch=None, |
| 64 | + pct_start=0.3, |
| 65 | + anneal_strategy='cos', |
| 66 | + cycle_momentum=True, |
| 67 | + base_momentum=0.85, |
| 68 | + max_momentum=0.95, |
| 69 | + div_factor=25.0, |
| 70 | + final_div_factor=10000.0, |
| 71 | + last_epoch=-1): |
| 72 | + """ |
| 73 | +
|
| 74 | + Parameters |
| 75 | + ---------- |
| 76 | + optimizer (Optimizer): Wrapped optimizer. |
| 77 | + max_lr (float or list): Upper learning rate boundaries in the cycle |
| 78 | + for each parameter group. |
| 79 | + total_steps (int): The total number of steps in the cycle. Note |
| 80 | + that if a value is provided here, then it must be inferred by |
| 81 | + providing a value for epochs and steps_per_epoch. |
| 82 | + Default: None |
| 83 | + epochs (int): The number of epochs to train for. This is used along |
| 84 | + with steps_per_epoch in order to infer the total number of |
| 85 | + steps in the cycle if a value for total_steps is not provided. |
| 86 | + Default: None |
| 87 | + steps_per_epoch (int): The number of steps per epoch to train for. |
| 88 | + This is used along with epochs in order to infer the total |
| 89 | + number of steps in the cycle if a value for total_steps is |
| 90 | + not provided. |
| 91 | + Default: None |
| 92 | + pct_start (float): The percentage of the cycle (in number of steps) |
| 93 | + spent increasing the learning rate. |
| 94 | + Default: 0.3 |
| 95 | + anneal_strategy (str): {'cos', 'linear'} |
| 96 | + Specifies the annealing strategy. |
| 97 | + Default: 'cos' |
| 98 | + cycle_momentum (bool): If ``True``, momentum is cycled inversely |
| 99 | + to learning rate between 'base_momentum' and 'max_momentum'. |
| 100 | + Default: True |
| 101 | + base_momentum (float or list): Lower momentum boundaries in the |
| 102 | + cycle for each parameter group. Note that momentum is cycled |
| 103 | + inversely to learning rate; at the peak of a cycle, momentum is |
| 104 | + 'base_momentum' and learning rate is 'max_lr'. |
| 105 | + Default: 0.85 |
| 106 | + max_momentum (float or list): Upper momentum boundaries in the |
| 107 | + cycle for each parameter group. Functionally, |
| 108 | + it defines the cycle amplitude (max_momentum - base_momentum). |
| 109 | + Note that momentum is cycled inversely |
| 110 | + to learning rate; at the start of a cycle, momentum is |
| 111 | + 'max_momentum' and learning rate is 'base_lr' |
| 112 | + Default: 0.95 |
| 113 | + div_factor (float): Determines the initial learning rate via |
| 114 | + initial_lr = max_lr/div_factor |
| 115 | + Default: 25 |
| 116 | + final_div_factor (float): Determines the minimum learning rate via |
| 117 | + min_lr = initial_lr/final_div_factor |
| 118 | + Default: 1e4 |
| 119 | + last_epoch (int): The index of the last batch. This parameter is |
| 120 | + used when resuming a training job. Since `step()` should be |
| 121 | + invoked after each batch instead of after each epoch, this |
| 122 | + number represents the total number of *batches* computed, |
| 123 | + not the total number of epochs computed. |
| 124 | + When last_epoch=-1, the schedule is started from the |
| 125 | + beginning. |
| 126 | + Default: -1 |
| 127 | + """ |
| 128 | + super().__init__() |
| 129 | + self.scheduler = OneCycleLR( |
| 130 | + optimizer, |
| 131 | + max_lr, |
| 132 | + total_steps, |
| 133 | + epochs, |
| 134 | + steps_per_epoch, |
| 135 | + pct_start, |
| 136 | + anneal_strategy, |
| 137 | + cycle_momentum, |
| 138 | + base_momentum, |
| 139 | + max_momentum, |
| 140 | + div_factor, |
| 141 | + final_div_factor, |
| 142 | + last_epoch) |
| 143 | + |
| 144 | + def at_iter_begin(self, trainer, train, |
| 145 | + **kwargs): |
| 146 | + """ |
| 147 | + Executes a single scheduling step |
| 148 | +
|
| 149 | + Parameters |
| 150 | + ---------- |
| 151 | + trainer : :class:`PyTorchNetworkTrainer` |
| 152 | + the trainer class, which can be changed |
| 153 | + kwargs : |
| 154 | + additional keyword arguments |
| 155 | +
|
| 156 | + Returns |
| 157 | + ------- |
| 158 | + :class:`PyTorchNetworkTrainer` |
| 159 | + modified trainer |
| 160 | +
|
| 161 | + """ |
| 162 | + if train: |
| 163 | + self.scheduler.step() |
| 164 | + |
| 165 | + return {} |
| 166 | + |
| 167 | + def at_epoch_end(self, trainer, **kwargs): |
| 168 | + return {} |
| 169 | + |
50 | 170 | class ReduceLROnPlateauCallback(DefaultPyTorchSchedulerCallback): |
51 | 171 | """ |
52 | 172 | Wraps PyTorch's `ReduceLROnPlateau` Scheduler as Callback |
|
0 commit comments