Create a schedule with a learning rate that decreases linearly from the initial lr set in the optimizer to 0, after a warmup period during which it increases linearly from 0 to the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The op
(
optimizer: Optimizer, num_warmup_steps: int, num_training_steps: int, last_epoch: int = -1
)
| 121 | |
| 122 | |
| 123 | def get_linear_schedule_with_warmup( |
| 124 | optimizer: Optimizer, num_warmup_steps: int, num_training_steps: int, last_epoch: int = -1 |
| 125 | ) -> LambdaLR: |
| 126 | """ |
| 127 | Create a schedule with a learning rate that decreases linearly from the initial lr set in the optimizer to 0, after |
| 128 | a warmup period during which it increases linearly from 0 to the initial lr set in the optimizer. |
| 129 | |
| 130 | Args: |
| 131 | optimizer ([`~torch.optim.Optimizer`]): |
| 132 | The optimizer for which to schedule the learning rate. |
| 133 | num_warmup_steps (`int`): |
| 134 | The number of steps for the warmup phase. |
| 135 | num_training_steps (`int`): |
| 136 | The total number of training steps. |
| 137 | last_epoch (`int`, *optional*, defaults to -1): |
| 138 | The index of the last epoch when resuming training. |
| 139 | |
| 140 | Return: |
| 141 | `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. |
| 142 | """ |
| 143 | |
| 144 | def lr_lambda(current_step: int): |
| 145 | if current_step < num_warmup_steps: |
| 146 | return float(current_step) / float(max(1, num_warmup_steps)) |
| 147 | return max( |
| 148 | 0.0, float(num_training_steps - current_step) / float(max(1, num_training_steps - num_warmup_steps)) |
| 149 | ) |
| 150 | |
| 151 | return LambdaLR(optimizer, lr_lambda, last_epoch) |
| 152 | |
| 153 | |
| 154 | def get_cosine_schedule_with_warmup( |
nothing calls this directly
no outgoing calls
no test coverage detected