MCPcopy Create free account
hub / github.com/AtlasAnalyticsLab/AdaFisher / MultiStepLR

Class MultiStepLR

optimizers/lr_scheduler.py:435–478  ·  view source on GitHub ↗

Decays the learning rate of each parameter group by gamma once the number of epoch reaches one of the milestones. 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:

Source from the content-addressed store, hash-verified

433
434
435class MultiStepLR(_LRScheduler):
436 """Decays the learning rate of each parameter group by gamma once the
437 number of epoch reaches one of the milestones. Notice that such decay can
438 happen simultaneously with other changes to the learning rate from outside
439 this scheduler. When last_epoch=-1, sets initial lr as lr.
440
441 Args:
442 optimizer (Optimizer): Wrapped optimizer.
443 milestones (list): List of epoch indices. Must be increasing.
444 gamma (float): Multiplicative factor of learning rate decay.
445 Default: 0.1.
446 last_epoch (int): The index of last epoch. Default: -1.
447
448 Example:
449 >>> # Assuming optimizer uses lr = 0.05 for all groups
450 >>> # lr = 0.05 if epoch < 30
451 >>> # lr = 0.005 if 30 <= epoch < 80
452 >>> # lr = 0.0005 if epoch >= 80
453 >>> scheduler = MultiStepLR(optimizer, milestones=[30,80], gamma=0.1)
454 >>> for epoch in range(100):
455 >>> train(...)
456 >>> validate(...)
457 >>> scheduler.step()
458 """
459
460 def __init__(self, optimizer, milestones, gamma=0.1, last_epoch=-1):
461 self.milestones = Counter(milestones)
462 self.gamma = gamma
463 super(MultiStepLR, self).__init__(optimizer, last_epoch)
464
465 def get_lr(self):
466 if not self._get_lr_called_within_step:
467 warnings.warn("To get the last learning rate computed by the scheduler, "
468 "please use `get_last_lr()`.", UserWarning)
469
470 if self.last_epoch not in self.milestones:
471 return [group['lr'] for group in self.optimizer.param_groups]
472 return [group['lr'] * self.gamma ** self.milestones[self.last_epoch]
473 for group in self.optimizer.param_groups]
474
475 def _get_closed_form_lr(self):
476 milestones = list(sorted(self.milestones.elements()))
477 return [base_lr * self.gamma ** bisect_right(milestones, self.last_epoch)
478 for base_lr in self.base_lrs]
479
480
481class ExponentialLR(_LRScheduler):

Callers 1

get_optimizer_schedulerFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected