3 from .optimizer
import Optimizer
7 """Implements Averaged Stochastic Gradient Descent. 9 It has been proposed in `Acceleration of stochastic approximation by 13 params (iterable): iterable of parameters to optimize or dicts defining 15 lr (float, optional): learning rate (default: 1e-2) 16 lambd (float, optional): decay term (default: 1e-4) 17 alpha (float, optional): power for eta update (default: 0.75) 18 t0 (float, optional): point at which to start averaging (default: 1e6) 19 weight_decay (float, optional): weight decay (L2 penalty) (default: 0) 21 .. _Acceleration of stochastic approximation by averaging: 22 http://dl.acm.org/citation.cfm?id=131098 25 def __init__(self, params, lr=1e-2, lambd=1e-4, alpha=0.75, t0=1e6, weight_decay=0):
27 raise ValueError(
"Invalid learning rate: {}".format(lr))
28 if not 0.0 <= weight_decay:
29 raise ValueError(
"Invalid weight_decay value: {}".format(weight_decay))
31 defaults = dict(lr=lr, lambd=lambd, alpha=alpha, t0=t0,
32 weight_decay=weight_decay)
33 super(ASGD, self).__init__(params, defaults)
35 def step(self, closure=None):
36 """Performs a single optimization step. 39 closure (callable, optional): A closure that reevaluates the model 43 if closure
is not None:
46 for group
in self.param_groups:
47 for p
in group[
'params']:
52 raise RuntimeError(
'ASGD does not support sparse gradients')
58 state[
'eta'] = group[
'lr']
60 state[
'ax'] = torch.zeros_like(p.data)
64 if group[
'weight_decay'] != 0:
65 grad = grad.add(group[
'weight_decay'], p.data)
68 p.data.mul_(1 - group[
'lambd'] * state[
'eta'])
71 p.data.add_(-state[
'eta'], grad)
75 state[
'ax'].add_(p.data.sub(state[
'ax']).mul(state[
'mu']))
77 state[
'ax'].copy_(p.data)
80 state[
'eta'] = (group[
'lr'] /
81 math.pow((1 + group[
'lambd'] * group[
'lr'] * state[
'step']), group[
'alpha']))
82 state[
'mu'] = 1 / max(1, state[
'step'] - group[
't0'])
def step(self, closure=None)