-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_sgd_lop_sgdn.py
172 lines (138 loc) · 6.47 KB
/
op_sgd_lop_sgdn.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import torch
from functools import reduce
from torch.optim.optimizer import Optimizer, required
class op_Sgd_lop_Sgdn(Optimizer):
r"""Implements stochastic gradient descent (optionally with momentum).
Nesterov momentum is based on the formula from
`On the importance of initialization and momentum in deep learning`__.
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)
hypergrad_lr (float, optional): hypergradient learning rate for the online
tuning of the learning rate, introduced in the paper
`Online Learning Rate Adaptation with Hypergradient Descent`_
Example:
>>> optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9)
>>> optimizer.zero_grad()
>>> loss_fn(model(input), target).backward()
>>> optimizer.step()
__ http://www.cs.toronto.edu/%7Ehinton/absps/momentum.pdf
.. _Online Learning Rate Adaptation with Hypergradient Descent:
https://openreview.net/forum?id=BkrsAzWAb
.. note::
The implementation of SGD with Momentum/Nesterov subtly differs from
Sutskever et. al. and implementations in some other frameworks.
Considering the specific case of Momentum, the update can be written as
.. math::
v = \rho * v + g \\
p = p - lr * v
where p, g, v and :math:`\rho` denote the parameters, gradient,
velocity, and momentum respectively.
This is in contrast to Sutskever et. al. and
other frameworks which employ an update of the form
.. math::
v = \rho * v + lr * g \\
p = p - v
The Nesterov version is analogously modified.
"""
def __init__(self, params, lr=required, momentum=0, dampening=0, momentum_h=0.9, dampening_h=0, nesterov_h=True,
weight_decay=0, nesterov=False, hypergrad_lr=1e-6):
defaults = dict(lr=lr, momentum=momentum, dampening=dampening, momentum_h=momentum_h, dampening_h = dampening_h, nesterov_h=nesterov_h,
weight_decay=weight_decay, nesterov=nesterov, hypergrad_lr=hypergrad_lr)
if nesterov and (momentum <= 0 or dampening != 0):
raise ValueError("Nesterov momentum requires a momentum and zero dampening")
super(op_Sgd_lop_Sgdn, self).__init__(params, defaults)
if len(self.param_groups) != 1:
raise ValueError("op_Sgd_lop_Sgdn doesn't support per-parameter options (parameter groups)")
self._params = self.param_groups[0]['params']
self._params_numel = reduce(lambda total, p: total + p.numel(), self._params, 0)
def _gather_flat_grad_with_weight_decay(self, weight_decay=0):
views = []
for p in self._params:
if p.grad is None:
view = torch.zeros_like(p.data)
elif p.grad.data.is_sparse:
view = p.grad.data.to_dense().view(-1)
else:
view = p.grad.data.view(-1)
if weight_decay != 0:
view.add_(weight_decay, p.data.view(-1))
views.append(view)
return torch.cat(views, 0)
def _add_grad(self, step_size, update):
offset = 0
for p in self._params:
numel = p.numel()
# view as to avoid deprecated pointwise semantics
p.data.add_(step_size, update[offset:offset + numel].view_as(p.data))
offset += numel
assert offset == self._params_numel
def step(self, closure=None):
"""Performs a single optimization step.
Arguments:
closure (callable, optional): A closure that reevaluates the model
and returns the loss.
"""
assert len(self.param_groups) == 1
loss = None
if closure is not None:
loss = closure()
group = self.param_groups[0]
weight_decay = group['weight_decay']
momentum = group['momentum']
dampening = group['dampening']
nesterov = group['nesterov']
grad = self._gather_flat_grad_with_weight_decay(weight_decay)
# NOTE: op_Sgd_lop_Sgdn has only global state, but we register it as state for
# the first param, because this helps with casting in load_state_dict
state = self.state[self._params[0]]
# State initialization
if len(state) == 0:
state['step'] = 0
state['grad_prev'] = torch.zeros_like(grad)
# Accumulated momentum for the hypergradient
state['momentum_buffer_h'] = grad.new_tensor(0)
state['step']+=1
grad_prev = state['grad_prev']
# Hypergradient for SGD optimizer
h = torch.dot(grad, grad_prev)
h = -h
''' Hypergradient descent with momentum (HD-momentum) coefficients
Parameters
-----------
momentum_h : momentum coefficient for the hypergradient
dampening_h : dampening coefficient for the hypergradient
nesterov_h : bool, if true : use nesterov momentum for the l.r update, else use sgd + momemtum
'''
momentum_h = group['momentum_h']
dampening_h = group['dampening_h']
nesterov_h = group['nesterov_h']
# Hypergradient descent with momentum (HD momentum) for the learning rate
if momentum_h and state['step'] > 1:
buf_h = state['momentum_buffer_h']
buf_h.mul_(momentum_h).add_(1 - dampening_h, h)
state['momentum_buffer_h'] = buf_h
if nesterov_h:
h.add_(momentum_h, buf_h)
else:
h = buf_h
group['lr'] -= group['hypergrad_lr'] * h
if momentum != 0:
if 'momentum_buffer' not in state:
buf = state['momentum_buffer'] = torch.zeros_like(grad)
buf.mul_(momentum).add_(grad)
else:
buf = state['momentum_buffer']
buf.mul_(momentum).add_(1 - dampening, grad)
if nesterov:
grad.add_(momentum, buf)
else:
grad = buf
state['grad_prev'] = grad
self._add_grad(-group['lr'], grad)
return loss