Decays the learning rate of each parameter group by gamma every step_size epochs. Notice that such decay can happen simultaneously with other changes to the learning rate from outside this scheduler. When last_epoch=-1, sets initial lr as lr. Args: optimizer (Optimizer): Wra
| 387 | |
| 388 | |
| 389 | class StepLR(_LRScheduler): |
| 390 | """Decays the learning rate of each parameter group by gamma every |
| 391 | step_size epochs. Notice that such decay can happen simultaneously with |
| 392 | other changes to the learning rate from outside this scheduler. When |
| 393 | last_epoch=-1, sets initial lr as lr. |
| 394 | |
| 395 | Args: |
| 396 | optimizer (Optimizer): Wrapped optimizer. |
| 397 | step_size (int): Period of learning rate decay. |
| 398 | gamma (float): Multiplicative factor of learning rate decay. |
| 399 | Default: 0.1. |
| 400 | last_epoch (int): The index of last epoch. Default: -1. |
| 401 | |
| 402 | Example: |
| 403 | >>> # Assuming optimizer uses lr = 0.05 for all groups |
| 404 | >>> # lr = 0.05 if epoch < 30 |
| 405 | >>> # lr = 0.005 if 30 <= epoch < 60 |
| 406 | >>> # lr = 0.0005 if 60 <= epoch < 90 |
| 407 | >>> # ... |
| 408 | >>> scheduler = StepLR(optimizer, step_size=30, gamma=0.1) |
| 409 | >>> for epoch in range(100): |
| 410 | >>> train(...) |
| 411 | >>> validate(...) |
| 412 | >>> scheduler.step() |
| 413 | """ |
| 414 | |
| 415 | def __init__(self, optimizer, step_size, gamma=0.1, last_epoch=-1): |
| 416 | self.step_size = step_size |
| 417 | self.gamma = gamma |
| 418 | super(StepLR, self).__init__(optimizer, last_epoch) |
| 419 | |
| 420 | def get_lr(self): |
| 421 | if not self._get_lr_called_within_step: |
| 422 | warnings.warn("To get the last learning rate computed by the scheduler, " |
| 423 | "please use `get_last_lr()`.", UserWarning) |
| 424 | |
| 425 | if (self.last_epoch == 0) or (self.last_epoch % self.step_size != 0): |
| 426 | return [group['lr'] for group in self.optimizer.param_groups] |
| 427 | return [group['lr'] * self.gamma |
| 428 | for group in self.optimizer.param_groups] |
| 429 | |
| 430 | def _get_closed_form_lr(self): |
| 431 | return [base_lr * self.gamma ** (self.last_epoch // self.step_size) |
| 432 | for base_lr in self.base_lrs] |
| 433 | |
| 434 | |
| 435 | class MultiStepLR(_LRScheduler): |
no outgoing calls
no test coverage detected