|
1 | 1 | # pytorch-lr-scheduler
|
2 | 2 | PyTorch implementation of some learning rate schedulers for deep learning researcher.
|
| 3 | + |
| 4 | +## Usage |
| 5 | + |
| 6 | +### `ReduceLROnPlateauScheduler` |
| 7 | + |
| 8 | +- Example code |
| 9 | + |
| 10 | +```python |
| 11 | +import torch |
| 12 | + |
| 13 | +from lr_scheduler.reduce_lr_on_plateau_lr_scheduler import ReduceLROnPlateauScheduler |
| 14 | + |
| 15 | +if __name__ == '__main__': |
| 16 | + max_epochs, steps_in_epoch = 10, 10000 |
| 17 | + |
| 18 | + model = [torch.nn.Parameter(torch.randn(2, 2, requires_grad=True))] |
| 19 | + optimizer = torch.optim.Adam(model, 1e-4) |
| 20 | + |
| 21 | + scheduler = ReduceLROnPlateauScheduler(optimizer, patience=1, factor=0.3) |
| 22 | + |
| 23 | + for epoch in range(max_epochs): |
| 24 | + for timestep in range(steps_in_epoch): |
| 25 | + ... |
| 26 | + ... |
| 27 | + |
| 28 | + val_loss = validate() |
| 29 | + scheduler.step(val_loss) |
| 30 | +``` |
| 31 | + |
| 32 | +- Visualize |
| 33 | + |
| 34 | + |
| 35 | +### `TransformerLRScheduler` |
| 36 | + |
| 37 | +- Example code |
| 38 | + |
| 39 | +```python |
| 40 | +import torch |
| 41 | + |
| 42 | +from lr_scheduler.transformer_lr_scheduler import TransformerLRScheduler |
| 43 | + |
| 44 | +if __name__ == '__main__': |
| 45 | + max_epochs, steps_in_epoch = 10, 10000 |
| 46 | + |
| 47 | + model = [torch.nn.Parameter(torch.randn(2, 2, requires_grad=True))] |
| 48 | + optimizer = torch.optim.Adam(model, 1e-10) |
| 49 | + |
| 50 | + scheduler = TransformerLRScheduler( |
| 51 | + optimizer=optimizer, |
| 52 | + init_lr=1e-10, |
| 53 | + peak_lr=0.1, |
| 54 | + final_lr=1e-4, |
| 55 | + final_lr_scale=0.05, |
| 56 | + warmup_steps=3000, |
| 57 | + decay_steps=17000, |
| 58 | + ) |
| 59 | + |
| 60 | + for epoch in range(max_epochs): |
| 61 | + for timestep in range(steps_in_epoch): |
| 62 | + ... |
| 63 | + ... |
| 64 | + scheduler.step() |
| 65 | +``` |
| 66 | + |
| 67 | +- Visualize |
| 68 | + |
| 69 | + |
| 70 | +### `TriStageLRScheduler` |
| 71 | + |
| 72 | +- Example code |
| 73 | + |
| 74 | +```python |
| 75 | +import torch |
| 76 | + |
| 77 | +from lr_scheduler.tri_stage_lr_scheduler import TriStageLRScheduler |
| 78 | + |
| 79 | +if __name__ == '__main__': |
| 80 | + max_epochs, steps_in_epoch = 10, 10000 |
| 81 | + |
| 82 | + model = [torch.nn.Parameter(torch.randn(2, 2, requires_grad=True))] |
| 83 | + optimizer = torch.optim.Adam(model, 1e-10) |
| 84 | + |
| 85 | + scheduler = TriStageLRScheduler( |
| 86 | + optimizer, |
| 87 | + init_lr=1e-10, |
| 88 | + peak_lr=1e-4, |
| 89 | + final_lr=1e-7, |
| 90 | + init_lr_scale=0.01, |
| 91 | + final_lr_scale=0.05, |
| 92 | + warmup_steps=30000, |
| 93 | + hold_steps=70000, |
| 94 | + decay_steps=100000, |
| 95 | + total_steps=200000, |
| 96 | + ) |
| 97 | + |
| 98 | + for epoch in range(max_epochs): |
| 99 | + for timestep in range(steps_in_epoch): |
| 100 | + ... |
| 101 | + ... |
| 102 | + scheduler.step() |
| 103 | +``` |
| 104 | + |
| 105 | +- Visualize |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +### `WarmupReduceLROnPlateauScheduler` |
| 110 | + |
| 111 | +- Example code |
| 112 | +```python |
| 113 | +import torch |
| 114 | + |
| 115 | +from lr_scheduler.warmup_reduce_lr_on_plateau_scheduler import WarmupReduceLROnPlateauScheduler |
| 116 | + |
| 117 | +if __name__ == '__main__': |
| 118 | + max_epochs, steps_in_epoch = 10, 10000 |
| 119 | + |
| 120 | + model = [torch.nn.Parameter(torch.randn(2, 2, requires_grad=True))] |
| 121 | + optimizer = torch.optim.Adam(model, 1e-10) |
| 122 | + |
| 123 | + scheduler = WarmupReduceLROnPlateauScheduler( |
| 124 | + optimizer, |
| 125 | + init_lr=1e-10, |
| 126 | + peak_lr=1e-4, |
| 127 | + warmup_steps=30000, |
| 128 | + patience=1, |
| 129 | + factor=0.3, |
| 130 | + ) |
| 131 | + |
| 132 | + for epoch in range(max_epochs): |
| 133 | + for timestep in range(steps_in_epoch): |
| 134 | + ... |
| 135 | + ... |
| 136 | + if timestep < warmup_steps: |
| 137 | + scheduler.step() |
| 138 | + |
| 139 | + val_loss = validate() |
| 140 | + scheduler.step(val_loss) |
| 141 | +``` |
| 142 | + |
| 143 | +- Visualize |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +### `WarmupLRScheduler` |
| 148 | + |
| 149 | +- Example code |
| 150 | + |
| 151 | +```python |
| 152 | +import torch |
| 153 | + |
| 154 | +from lr_scheduler.warmup_lr_scheduler import WarmupLRScheduler |
| 155 | + |
| 156 | +if __name__ == '__main__': |
| 157 | + max_epochs, steps_in_epoch = 10, 10000 |
| 158 | + |
| 159 | + model = [torch.nn.Parameter(torch.randn(2, 2, requires_grad=True))] |
| 160 | + optimizer = torch.optim.Adam(model, 1e-10) |
| 161 | + |
| 162 | + scheduler = WarmupLRScheduler( |
| 163 | + optimizer, |
| 164 | + init_lr=1e-10, |
| 165 | + peak_lr=1e-4, |
| 166 | + warmup_steps=4000, |
| 167 | + ) |
| 168 | + |
| 169 | + for epoch in range(max_epochs): |
| 170 | + for timestep in range(steps_in_epoch): |
| 171 | + ... |
| 172 | + ... |
| 173 | + scheduler.step() |
| 174 | +``` |
| 175 | + |
| 176 | +- Visualize |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | +## Troubleshoots and Contributing |
| 181 | +If you have any questions, bug reports, and feature requests, please [open an issue](https://github.com/sooftware/openspeech/issues) on Github. |
| 182 | + |
| 183 | +I appreciate any kind of feedback or contribution. Feel free to proceed with small issues like bug fixes, documentation improvement. For major contributions and new features, please discuss with the collaborators in corresponding issues. |
| 184 | + |
| 185 | +### Code Style |
| 186 | +I follow [PEP-8](https://www.python.org/dev/peps/pep-0008/) for code style. Especially the style of docstrings is important to generate documentation. |
| 187 | + |
| 188 | +### License |
| 189 | +This project is licensed under the MIT LICENSE - see the [LICENSE.md](https://github.com/sooftware/OpenSpeech/blob/master/LICENSE) file for details |
0 commit comments