MCPcopy Create free account
hub / github.com/AtlasAnalyticsLab/AdaFisher / step

Method step

optimizers/sgd.py:150–189  ·  view source on GitHub ↗

Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss.

(self, closure=None)

Source from the content-addressed store, hash-verified

148
149 @torch.no_grad()
150 def step(self, closure=None):
151 """Performs a single optimization step.
152
153 Arguments:
154 closure (callable, optional): A closure that reevaluates the model
155 and returns the loss.
156 """
157 loss = None
158 if closure is not None:
159 with torch.enable_grad():
160 loss = closure()
161
162 for group in self.param_groups:
163 weight_decay = group['weight_decay']
164 momentum = group['momentum']
165 dampening = group['dampening']
166 nesterov = group['nesterov']
167
168 for p in group['params']:
169 if p.grad is None:
170 continue
171 d_p = p.grad
172 if weight_decay != 0:
173 d_p = d_p.add(p, alpha=weight_decay)
174 if momentum != 0:
175 param_state = self.state[p]
176 if 'momentum_buffer' not in param_state:
177 buf = param_state['momentum_buffer'] = torch.clone(
178 d_p).detach()
179 else:
180 buf = param_state['momentum_buffer']
181 buf.mul_(momentum).add_(d_p, alpha=1 - dampening)
182 if nesterov:
183 d_p = d_p.add(buf, alpha=momentum)
184 else:
185 d_p = buf
186
187 p.data.add_(d_p, alpha=-group['lr'])
188
189 return loss
190
191
192class SGDVec(Optimizer):

Callers 3

run_epochsMethod · 0.45
epoch_iterationMethod · 0.45
trainFunction · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected