Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss.
(self, closure=None)
| 167 | group.setdefault('amsgrad', False) |
| 168 | |
| 169 | def step(self, closure=None): |
| 170 | """Performs a single optimization step. |
| 171 | Arguments: |
| 172 | closure (callable, optional): A closure that reevaluates the model |
| 173 | and returns the loss. |
| 174 | """ |
| 175 | loss = None |
| 176 | if closure is not None: |
| 177 | loss = closure() |
| 178 | |
| 179 | for group in self.param_groups: |
| 180 | for p in group['params']: |
| 181 | if p.grad is None: |
| 182 | continue |
| 183 | grad = p.grad.data |
| 184 | if grad.is_sparse: |
| 185 | raise RuntimeError('Adam does not support sparse gradients, please consider SparseAdam instead') |
| 186 | amsgrad = group['amsgrad'] |
| 187 | |
| 188 | state = self.state[p] |
| 189 | |
| 190 | # State initialization |
| 191 | if len(state) == 0: |
| 192 | state['step'] = 0 |
| 193 | # Exponential moving average of gradient values |
| 194 | state['exp_avg'] = torch.zeros_like(p.data) |
| 195 | # Exponential moving average of squared gradient values |
| 196 | state['exp_avg_sq'] = torch.zeros_like(p.data) |
| 197 | if amsgrad: |
| 198 | # Maintains max of all exp. moving avg. of sq. grad. values |
| 199 | state['max_exp_avg_sq'] = torch.zeros_like(p.data) |
| 200 | |
| 201 | exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq'] |
| 202 | if amsgrad: |
| 203 | max_exp_avg_sq = state['max_exp_avg_sq'] |
| 204 | beta1, beta2 = group['betas'] |
| 205 | |
| 206 | state['step'] += 1 |
| 207 | |
| 208 | # MODIFIED HERE |
| 209 | #if group['weight_decay'] != 0: |
| 210 | # grad = grad.add(group['weight_decay'], p.data) |
| 211 | |
| 212 | # Decay the first and second moment running average coefficient |
| 213 | exp_avg.mul_(beta1).add_(1 - beta1, grad) |
| 214 | exp_avg_sq.mul_(beta2).addcmul_(1 - beta2, grad, grad) |
| 215 | if amsgrad: |
| 216 | # Maintains the maximum of all 2nd moment running avg. till now |
| 217 | torch.max(max_exp_avg_sq, exp_avg_sq, out=max_exp_avg_sq) |
| 218 | # Use the max. for normalizing running avg. of gradient |
| 219 | denom = max_exp_avg_sq.sqrt().add_(group['eps']) |
| 220 | else: |
| 221 | denom = exp_avg_sq.sqrt().add_(group['eps']) |
| 222 | |
| 223 | bias_correction1 = 1 - beta1 ** state['step'] |
| 224 | bias_correction2 = 1 - beta2 ** state['step'] |
| 225 | step_size = group['lr'] * math.sqrt(bias_correction2) / bias_correction1 |
| 226 |
no outgoing calls
no test coverage detected