站内公告:
2024-04-22 14:29:54 点击量:
import torch from torch.optim.optimizer import Optimizer, required class SGD(Optimizer): def __init__(self, params, lr=required, momentum=0, dampening=0, weight_decay1=0, weight_decay2=0, nesterov=False): defaults =dict(lr=lr, momentum=momentum, dampening=dampening, weight_decay1=weight_decay1, weight_decay2=weight_decay2, nesterov=nesterov) if nesterov and (momentum <=0 or dampening != 0): raise ValueError("Nesterov momentum requires a momentum and zero dampening") super(SGD, self).__init__(params, defaults) def __setstate__(self, state): super(SGD, self).__setstate__(state) for group in self.param_groups: group.setdefault('nesterov', False) def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() for group in self.param_groups: weight_decay1 =group['weight_decay1'] weight_decay2 =group['weight_decay2'] 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_decay1 != 0: d_p.add_(weight_decay1, torch.sign(p.data)) if weight_decay2 != 0: d_p.add_(weight_decay2, 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
Copyright © 2012-2018 首页-杏福娱乐-杏福商务站
地址:海南省海口市玉沙路58号电话:0898-88889999手机:13988889999
ICP备案编号:琼ICP备88889999号
微信扫一扫