Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss.
(self, layers_index_todo, lr_vector, closure=None)
| 259 | group.setdefault('nesterov', False) |
| 260 | |
| 261 | def step(self, layers_index_todo, lr_vector, closure=None): |
| 262 | """Performs a single optimization step. |
| 263 | |
| 264 | Arguments: |
| 265 | closure (callable, optional): A closure that reevaluates the model |
| 266 | and returns the loss. |
| 267 | """ |
| 268 | loss = None |
| 269 | if closure is not None: |
| 270 | loss = closure() |
| 271 | |
| 272 | iteration_group = 0 |
| 273 | for group in self.param_groups: |
| 274 | iteration_group += 1 |
| 275 | weight_decay = group['weight_decay'] |
| 276 | momentum = group['momentum'] |
| 277 | dampening = group['dampening'] |
| 278 | nesterov = group['nesterov'] |
| 279 | |
| 280 | iteration_p = 0 |
| 281 | for p in group['params']: |
| 282 | if p.grad is None or ~layers_index_todo[iteration_p]: |
| 283 | iteration_p += 1 |
| 284 | continue |
| 285 | d_p = p.grad.data |
| 286 | if weight_decay != 0: |
| 287 | d_p.add_(weight_decay, p.data) |
| 288 | if momentum != 0: |
| 289 | param_state = self.state[p] |
| 290 | if 'momentum_buffer' not in param_state: |
| 291 | buf = param_state['momentum_buffer'] = torch.clone( |
| 292 | d_p).detach() |
| 293 | else: |
| 294 | buf = param_state['momentum_buffer'] |
| 295 | buf.mul_(momentum).add_(1 - dampening, d_p) |
| 296 | if nesterov: |
| 297 | d_p = d_p.add(momentum, buf) |
| 298 | else: |
| 299 | d_p = buf |
| 300 | |
| 301 | # p.data.add_(-group['lr'], d_p) |
| 302 | p.data.add_(d_p, alpha=-lr_vector[iteration_p]) |
| 303 | iteration_p += 1 |
| 304 | |
| 305 | return loss |
nothing calls this directly
no outgoing calls
no test coverage detected