schedulers
sleap_nn.training.schedulers
¶
Custom learning rate schedulers for sleap-nn training.
This module provides learning rate schedulers with warmup phases that are commonly used in deep learning for pose estimation and computer vision tasks.
Classes:
| Name | Description |
|---|---|
LinearWarmupCosineAnnealingLR |
Cosine annealing learning rate scheduler with linear warmup. |
LinearWarmupLinearDecayLR |
Linear warmup followed by linear decay learning rate scheduler. |
LinearWarmupCosineAnnealingLR
¶
Bases: LRScheduler
Cosine annealing learning rate scheduler with linear warmup.
The learning rate increases linearly from warmup_start_lr to the optimizer's
base learning rate over warmup_epochs, then decreases following a cosine
curve to eta_min over the remaining epochs.
This schedule is widely used in vision transformers and modern CNN architectures as it provides stable early training (warmup) and smooth convergence (cosine decay).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
Wrapped optimizer. |
required | |
warmup_epochs
|
int
|
Number of epochs for the linear warmup phase. |
required |
max_epochs
|
int
|
Total number of training epochs. |
required |
warmup_start_lr
|
float
|
Learning rate at the start of warmup. Default: 0.0. |
0.0
|
eta_min
|
float
|
Minimum learning rate at the end of the schedule. Default: 0.0. |
0.0
|
last_epoch
|
int
|
The index of the last epoch. Default: -1. |
-1
|
Example
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3) scheduler = LinearWarmupCosineAnnealingLR( ... optimizer, warmup_epochs=5, max_epochs=100, eta_min=1e-6 ... ) for epoch in range(100): ... train(...) ... scheduler.step()
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the scheduler. |
get_lr |
Compute the learning rate at the current epoch. |
Source code in sleap_nn/training/schedulers.py
__init__(optimizer, warmup_epochs, max_epochs, warmup_start_lr=0.0, eta_min=0.0, last_epoch=-1)
¶
Initialize the scheduler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
Wrapped optimizer. |
required | |
warmup_epochs
|
int
|
Number of epochs for the linear warmup phase. |
required |
max_epochs
|
int
|
Total number of training epochs. |
required |
warmup_start_lr
|
float
|
Learning rate at the start of warmup. Default: 0.0. |
0.0
|
eta_min
|
float
|
Minimum learning rate at the end of the schedule. Default: 0.0. |
0.0
|
last_epoch
|
int
|
The index of the last epoch. Default: -1. |
-1
|
Source code in sleap_nn/training/schedulers.py
get_lr()
¶
Compute the learning rate at the current epoch.
Source code in sleap_nn/training/schedulers.py
LinearWarmupLinearDecayLR
¶
Bases: LRScheduler
Linear warmup followed by linear decay learning rate scheduler.
The learning rate increases linearly from warmup_start_lr to the optimizer's
base learning rate over warmup_epochs, then decreases linearly to end_lr
over the remaining epochs.
This schedule provides a simple, interpretable learning rate trajectory and is commonly used in transformer-based models and NLP tasks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
Wrapped optimizer. |
required | |
warmup_epochs
|
int
|
Number of epochs for the linear warmup phase. |
required |
max_epochs
|
int
|
Total number of training epochs. |
required |
warmup_start_lr
|
float
|
Learning rate at the start of warmup. Default: 0.0. |
0.0
|
end_lr
|
float
|
Learning rate at the end of training. Default: 0.0. |
0.0
|
last_epoch
|
int
|
The index of the last epoch. Default: -1. |
-1
|
Example
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3) scheduler = LinearWarmupLinearDecayLR( ... optimizer, warmup_epochs=5, max_epochs=100, end_lr=1e-6 ... ) for epoch in range(100): ... train(...) ... scheduler.step()
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the scheduler. |
get_lr |
Compute the learning rate at the current epoch. |
Source code in sleap_nn/training/schedulers.py
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 | |
__init__(optimizer, warmup_epochs, max_epochs, warmup_start_lr=0.0, end_lr=0.0, last_epoch=-1)
¶
Initialize the scheduler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optimizer
|
Wrapped optimizer. |
required | |
warmup_epochs
|
int
|
Number of epochs for the linear warmup phase. |
required |
max_epochs
|
int
|
Total number of training epochs. |
required |
warmup_start_lr
|
float
|
Learning rate at the start of warmup. Default: 0.0. |
0.0
|
end_lr
|
float
|
Learning rate at the end of training. Default: 0.0. |
0.0
|
last_epoch
|
int
|
The index of the last epoch. Default: -1. |
-1
|
Source code in sleap_nn/training/schedulers.py
get_lr()
¶
Compute the learning rate at the current epoch.