Exponentially increases the learning rate between two boundaries over a number of iterations. Arguments: optimizer (torch.optim.Optimizer): wrapped optimizer. end_lr (float): the final learning rate. num_iter (int): the number of iterations over which the test occurs.
| 1071 | |
| 1072 | |
| 1073 | class ExponentialLR(_LRScheduler): |
| 1074 | """Exponentially increases the learning rate between two boundaries over a number of |
| 1075 | iterations. |
| 1076 | Arguments: |
| 1077 | optimizer (torch.optim.Optimizer): wrapped optimizer. |
| 1078 | end_lr (float): the final learning rate. |
| 1079 | num_iter (int): the number of iterations over which the test occurs. |
| 1080 | last_epoch (int, optional): the index of last epoch. Default: -1. |
| 1081 | """ |
| 1082 | |
| 1083 | def __init__(self, optimizer, end_lr, num_iter, last_epoch=-1): |
| 1084 | self.end_lr = end_lr |
| 1085 | |
| 1086 | if num_iter <= 1: |
| 1087 | raise ValueError("`num_iter` must be larger than 1") |
| 1088 | self.num_iter = num_iter |
| 1089 | |
| 1090 | super(ExponentialLR, self).__init__(optimizer, last_epoch) |
| 1091 | |
| 1092 | def get_lr(self): |
| 1093 | # In earlier Pytorch versions last_epoch starts at -1, while in recent versions |
| 1094 | # it starts at 0. We need to adjust the math a bit to handle this. See |
| 1095 | # discussion at: https://github.com/davidtvs/pytorch-lr-finder/pull/42 |
| 1096 | if PYTORCH_VERSION < version.parse("1.1.0"): |
| 1097 | curr_iter = self.last_epoch + 1 |
| 1098 | r = curr_iter / (self.num_iter - 1) |
| 1099 | else: |
| 1100 | r = self.last_epoch / (self.num_iter - 1) |
| 1101 | |
| 1102 | return [base_lr * (self.end_lr / base_lr) ** r for base_lr in self.base_lrs] |
| 1103 | |
| 1104 | |
| 1105 | class StateCacher(object): |