MCPcopy Create free account
hub / github.com/geekcomputers/Python / step

Method step

ML/src/python/neuralforge/optim/optimizers.py:182–224  ·  view source on GitHub ↗
(self, closure=None)

Source from the content-addressed store, hash-verified

180 super().__init__(params, defaults)
181
182 def step(self, closure=None):
183 loss = None
184 if closure is not None:
185 loss = closure()
186
187 for group in self.param_groups:
188 for p in group['params']:
189 if p.grad is None:
190 continue
191
192 grad = p.grad.data
193 state = self.state[p]
194
195 if len(state) == 0:
196 state['step'] = 0
197 state['exp_avg'] = torch.zeros_like(p.data)
198 state['exp_avg_sq'] = torch.zeros_like(p.data)
199
200 exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq']
201 beta1, beta2 = group['betas']
202 state['step'] += 1
203
204 if group['weight_decay'] != 0:
205 grad.add_(p.data, alpha=group['weight_decay'])
206
207 exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1)
208 exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2)
209
210 bias_correction1 = 1 - beta1 ** state['step']
211 bias_correction2 = 1 - beta2 ** state['step']
212
213 step_size = group['lr'] * math.sqrt(bias_correction2) / bias_correction1
214
215 final_lr = group['final_lr'] * group['lr'] / group['lr']
216 lower_bound = final_lr * (1 - 1 / (group['gamma'] * state['step'] + 1))
217 upper_bound = final_lr * (1 + 1 / (group['gamma'] * state['step']))
218
219 denom = exp_avg_sq.sqrt().add_(group['eps'])
220 step_size_clipped = torch.full_like(denom, step_size).div_(denom).clamp_(lower_bound, upper_bound).mul_(exp_avg)
221
222 p.data.add_(step_size_clipped, alpha=-1)
223
224 return loss
225
226class Lookahead(Optimizer):
227 def __init__(self, optimizer, k=5, alpha=0.5):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected