Created
December 6, 2017 01:48
-
-
Save ceshine/9e028eccdaf8668b3538f7768ea737c9 to your computer and use it in GitHub Desktop.
PyTorch SGD implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://pytorch.org/docs/master/_modules/torch/optim/sgd.html#SGD | |
class SGD(Optimizer): | |
def __init__(self, params, lr=required, momentum=0, dampening=0, | |
weight_decay=0, nesterov=False): | |
# ... | |
def __setstate__(self, state): | |
# ... | |
def step(self, closure=None): | |
# ... | |
for group in self.param_groups: | |
weight_decay = group['weight_decay'] | |
momentum = group['momentum'] | |
dampening = group['dampening'] | |
nesterov = group['nesterov'] | |
for p in group['params']: | |
if p.grad is None: | |
continue | |
d_p = p.grad.data | |
if weight_decay != 0: | |
d_p.add_(weight_decay, p.data) | |
if momentum != 0: | |
param_state = self.state[p] | |
if 'momentum_buffer' not in param_state: | |
buf = param_state['momentum_buffer'] = torch.zeros_like(p.data) | |
buf.mul_(momentum).add_(d_p) | |
else: | |
buf = param_state['momentum_buffer'] | |
buf.mul_(momentum).add_(1 - dampening, d_p) | |
if nesterov: | |
d_p = d_p.add(momentum, buf) | |
else: | |
d_p = buf | |
p.data.add_(-group['lr'], d_p) | |
return loss |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment