-
Notifications
You must be signed in to change notification settings - Fork 16
/
sgd.py
66 lines (53 loc) · 2.44 KB
/
sgd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import torch
from torch.optim import SGD
class BatchNormSGD(SGD):
r"""Implements stochastic gradient descent (optionally with momentum).
Args:
params (iterable): iterable of parameters to optimize or dicts defining
parameter groups
lr (float): learning rate
momentum (float, optional): momentum factor (default: 0)
weight_decay (float, optional): weight decay (L2 penalty) (default: 0)
dampening (float, optional): dampening for momentum (default: 0)
nesterov (bool, optional): enables Nesterov momentum (default: False)
ista ([float], required) : list of ista penalties for each layer
"""
def __init__(self, params, ista, lr=0.1, momentum=0, dampening=0,
weight_decay=0, nesterov=False):
self.ista = ista
super(BatchNormSGD, self).__init__(params, lr=lr, momentum=momentum, dampening=dampening, weight_decay=weight_decay, nesterov=nesterov)
def update_ista(self, ista):
self.ista = ista
def step(self, closure=None):
loss = None
if closure is not None:
loss = closure()
# not allowed to use weight_decay, should add a check
for group in self.param_groups:
weight_decay = group['weight_decay']
momentum = group['momentum']
dampening = group['dampening']
nesterov = group['nesterov']
for p, ista in zip(group['params'], self.ista):
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'] = p.data.new().resize_as_(p.data).zero_()
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
# apply group lasso
x = p.data.add(-group['lr'],d_p)
x = torch.clamp((torch.abs(x) - ista), min=0.)
p.data = x * torch.sign(x)
return loss