LRScheduler¶
danling.optim.LRScheduler
¶
Bases: _LRScheduler
General learning rate scheduler.
PyTorch LRScheduler is hard to extend. This class is a wrapper of PyTorch LRScheduler, which provides a more general interface. You only needs to add a new method which calculates a learning rate ratio (range from 0 to 1) with total progress (range from 0 to 1), and everything else will be done automatically.
Moreover, this class has warmup and cooldown built-in.
By default, the first 5% and last 20% of training steps will be warmup and cooldown respectively.
You can alternate by passing warmup_steps
and cooldown_steps
, or disable them by setting them to 0.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Optimizer
|
Wrapped optimizer. |
required |
|
int
|
Total number of trainable steps. |
required |
|
Optional[float]
|
Final learning rate ratio to initial learning rate. Defaults to 1e-3. |
None
|
|
Optional[float]
|
Final learning rate. |
None
|
|
float
|
Minimal learning rate. Defaults to 1e-9. |
1e-09
|
|
str
|
Scaling strategy. Defaults to “cosine”. |
'cosine'
|
|
Optional[int]
|
Number of warmup steps.
Defaults to |
None
|
|
Optional[int]
|
Number of cooldown steps.
Defaults to |
None
|
|
int
|
The index of last epoch. Defaults to -1. |
-1
|
|
Optional[str]
|
Method to calculate learning rate given ratio, should be one of “percentile” or “numerical”.
Defaults to “percentile” if |
None
|
Examples:
>>> from danling.optim import LRScheduler
>>> import torch
>>> from torch import optim
>>> optimizer = optim.SGD([{'params': torch.tensor([0])}], lr=1, momentum=0.9)
>>> scheduler = LRScheduler(optimizer, total_steps=5, final_lr_ratio=1e-5, strategy='linear')
>>> lrs = []
>>> for epoch in range(5):
... lrs.append(scheduler.get_lr()[0])
... scheduler.step()
>>> [round(lr, 10) for lr in lrs]
[0.1, 0.01, 0.001, 0.0001, 1e-09]
>>> scheduler = LRScheduler(optimizer, total_steps=5, final_lr_ratio=1e-5, strategy='cosine')
>>> lrs = []
>>> for epoch in range(5):
... lrs.append(scheduler.get_lr()[0])
... scheduler.step()
>>> [round(lr, 10) for lr in lrs]
[0.3330753446, 0.0187302031, 0.000533897, 3.00232e-05, 1e-09]
>>> scheduler = LRScheduler(optimizer, total_steps=5, final_lr_ratio=1e-5, strategy='linear', method='numerical')
>>> lrs = []
>>> for epoch in range(5):
... lrs.append(scheduler.get_lr()[0])
... scheduler.step()
>>> [round(lr, 2) for lr in lrs]
[0.8, 0.6, 0.4, 0.2, 0.0]
Source code in danling/optim/lr_scheduler/lr_scheduler.py
Python | |
---|---|
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
|