(self, closure=None)
| 123 | super().__init__(params, defaults) |
| 124 | |
| 125 | def step(self, closure=None): |
| 126 | loss = None |
| 127 | if closure is not None: |
| 128 | loss = closure() |
| 129 | |
| 130 | for group in self.param_groups: |
| 131 | for p in group['params']: |
| 132 | if p.grad is None: |
| 133 | continue |
| 134 | |
| 135 | grad = p.grad.data |
| 136 | state = self.state[p] |
| 137 | |
| 138 | if len(state) == 0: |
| 139 | state['step'] = 0 |
| 140 | state['exp_avg'] = torch.zeros_like(p.data) |
| 141 | state['exp_avg_sq'] = torch.zeros_like(p.data) |
| 142 | |
| 143 | exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq'] |
| 144 | beta1, beta2 = group['betas'] |
| 145 | state['step'] += 1 |
| 146 | |
| 147 | exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1) |
| 148 | exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2) |
| 149 | |
| 150 | buffered = [[None, None, None] for _ in range(10)] |
| 151 | |
| 152 | rho_inf = 2 / (1 - beta2) - 1 |
| 153 | rho_t = rho_inf - 2 * state['step'] * (beta2 ** state['step']) / (1 - beta2 ** state['step']) |
| 154 | |
| 155 | if rho_t > 4: |
| 156 | bias_correction1 = 1 - beta1 ** state['step'] |
| 157 | bias_correction2 = 1 - beta2 ** state['step'] |
| 158 | |
| 159 | rt = math.sqrt( |
| 160 | (rho_t - 4) * (rho_t - 2) * rho_inf / ((rho_inf - 4) * (rho_inf - 2) * rho_t) |
| 161 | ) |
| 162 | |
| 163 | denom = (exp_avg_sq.sqrt() / math.sqrt(bias_correction2)).add_(group['eps']) |
| 164 | step_size = group['lr'] * rt / bias_correction1 |
| 165 | |
| 166 | p.data.addcdiv_(exp_avg, denom, value=-step_size) |
| 167 | else: |
| 168 | bias_correction1 = 1 - beta1 ** state['step'] |
| 169 | step_size = group['lr'] / bias_correction1 |
| 170 | p.data.add_(exp_avg, alpha=-step_size) |
| 171 | |
| 172 | if group['weight_decay'] != 0: |
| 173 | p.data.add_(p.data, alpha=-group['weight_decay'] * group['lr']) |
| 174 | |
| 175 | return loss |
| 176 | |
| 177 | class AdaBound(Optimizer): |
| 178 | def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), final_lr=0.1, gamma=1e-3, eps=1e-8, weight_decay=0): |
nothing calls this directly
no outgoing calls
no test coverage detected