-
Notifications
You must be signed in to change notification settings - Fork 1
/
functional.py
379 lines (314 loc) · 10.4 KB
/
functional.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
"""
"""
from typing import Iterable, List, Optional
import torch
from torch import Tensor
from torch_ecg.utils import add_docstring, remove_parameters_returns_from_docstring
__all__ = [
"prox_vr_sgd",
"prox_sgd",
"al_vr_sgd",
"al_sgd",
]
def prox_vr_sgd(
params: List[Tensor],
local_weights: Iterable[Tensor],
variance_buffer: Iterable[Tensor],
d_p_list: List[Tensor],
momentum_buffer_list: List[Optional[Tensor]],
*,
weight_decay: float,
momentum: float,
lr: float,
dampening: float,
nesterov: bool,
prox: float,
) -> None:
"""The function that executes the proximal SGD with variance reduction.
Mathematical definition:
.. math::
\\DeclareMathOperator*{\\argmin}{arg\\,min}
\\operatorname{prox}_{f / \\rho}(v) =
\\argmin_x \\{ f(x) + \\dfrac{\\rho}{2} \\lVert x-v \\rVert_2^2 \\}
Parameters
----------
params : List[torch.Tensor]
The parameters to optimize or dicts defining parameter groups.
local_weights : List[Tensor]
The local weights updated by the local optimizer,
or of the previous iteration,
i.e. the term :math:`v` in
.. math::
\\argmin_x \\{ f(x) + \\dfrac{\\rho}{2} \\lVert x-v \\rVert_2^2 \\}
variance_buffer : List[Tensor]
The variance buffers of the parameters,
used for variance reduction.
d_p_list : List[Tensor]
The list of gradients of the parameters.
momentum_buffer_list : List[Optional[Tensor]]
The list of momentum buffers.
weight_decay : float
Weight decay factor (L2 penalty).
momentum : float
Momentum factor.
lr : float
The learning rate.
dampening : float
Dampening for momentum.
nesterov : bool
If True, enables Nesterov momentum.
prox : float
The (penalty) coeff. of the proximal term,
i.e. the term :math:`\\rho` in
.. math::
\\argmin_x \\{ f(x) + \\dfrac{\\rho}{2} \\lVert x-v \\rVert_2^2 \\}
"""
if local_weights is None:
local_weights = [None] * len(params)
if variance_buffer is None:
variance_buffer = [None] * len(params)
for idx, (param, lw, vb) in enumerate(zip(params, local_weights, variance_buffer)):
d_p = d_p_list[idx]
if weight_decay != 0:
d_p = d_p.add(param, alpha=weight_decay) # L2 regularization
if prox != 0 and lw is not None:
d_p = d_p.add(param - lw.detach().clone(), alpha=prox) # proximal regularization
if momentum != 0:
buf = momentum_buffer_list[idx]
if buf is None:
buf = torch.clone(d_p).detach()
momentum_buffer_list[idx] = buf
else:
buf.mul_(momentum).add_(d_p, alpha=1 - dampening)
if nesterov:
d_p = d_p.add(buf, alpha=momentum)
else:
d_p = buf
if vb is not None:
d_p = d_p.sub(vb.detach().clone()) # variance reduction
param.add_(d_p, alpha=-lr)
@add_docstring(
remove_parameters_returns_from_docstring(prox_vr_sgd.__doc__, parameters="variance_buffer").replace(
"The function that executes the proximal SGD with variance reduction.",
"The function that executes the proximal SGD.",
)
)
def prox_sgd(
params: List[Tensor],
local_weights: Iterable[Tensor],
d_p_list: List[Tensor],
momentum_buffer_list: List[Optional[Tensor]],
*,
weight_decay: float,
momentum: float,
lr: float,
dampening: float,
nesterov: bool,
prox: float,
) -> None:
return prox_vr_sgd(
params,
local_weights,
None,
d_p_list,
momentum_buffer_list,
weight_decay=weight_decay,
momentum=momentum,
lr=lr,
dampening=dampening,
nesterov=nesterov,
prox=prox,
)
def al_vr_sgd(
params: List[Tensor],
local_weights: List[Tensor],
dual_weights: List[Tensor],
variance_buffer: List[Tensor],
d_p_list: List[Tensor],
momentum_buffer_list: List[Optional[Tensor]],
weight_decay: float,
momentum: float,
lr: float,
dampening: float,
nesterov: bool,
mu: float,
) -> None:
r"""The function that executes the augmented Lagrangian SGD with variance reduction
.. math::
\DeclareMathOperator*{\argmin}{arg\,min}
\argmin_x \mathcal{L}_{\mu}(x, x_0, \lambda) =
\argmin_x \{f(x) + \langle \lambda, x-x_0 \rangle + \dfrac{1}{2\mu} \lVert x-x_0 \rVert_2^2\}
Parameters
----------
params: list of dict or Parameter,
the parameters to optimize
local_weights: iterable of Parameter,
the (init) local weights,
i.e. the term `x_0` in
.. math::
\mathcal{L}_{\mu}(x, x_0, \lambda)
dual_weights: iterable of Parameter,
the weights of dual variables,
i.e. the term `\lambda` in
.. math::
\mathcal{L}_{\mu}(x, x_0, \lambda)
variance_buffer: list of Parameter, optional,
the variance buffers of the parameters,
used for variance reduction
d_p_list: list of Tensor,
the list of gradients of the parameters
momentum_buffer_list: list of Tensor or list of None,
the list of momentum buffers,
works only if `momentum` > 0
gradient_variance_buffer_list: list of Tensor or list of None,
the list of gradient variance buffers,
works only is `vr` is True
weight_decay: float,
weight decay (L2 penalty)
momentum: float,
momentum factor
lr: float, default 1e-3,
the learning rate
dampening: float,
dampening for momentum
nesterov: bool,
if True, enables Nesterov momentum
mu: float,
the (penalty) coeff. of the augmented Lagrangian term,
i.e. the term `\mu` in
.. math::
\mathcal{L}_{\mu}(x, x_0, \lambda)
"""
if variance_buffer is None:
variance_buffer = [None] * len(params)
for idx, (param, lw, dw, vb) in enumerate(zip(params, local_weights, dual_weights, variance_buffer)):
d_p = d_p_list[idx]
d_p = d_p.add(dw.detach().clone())
d_p = d_p.add(param - lw.detach().clone(), alpha=1 / mu)
if weight_decay != 0:
d_p = d_p.add(param, alpha=weight_decay) # L2 regularization
if momentum != 0:
buf = momentum_buffer_list[idx]
if buf is None:
buf = torch.clone(d_p).detach()
momentum_buffer_list[idx] = buf
else:
buf.mul_(momentum).add_(d_p, alpha=1 - dampening)
if nesterov:
d_p = d_p.add(buf, alpha=momentum)
else:
d_p = buf
if vb is not None:
d_p = d_p.sub(vb.detach().clone())
param.add_(d_p, alpha=-lr)
@add_docstring(
remove_parameters_returns_from_docstring(al_vr_sgd.__doc__, parameters="variance_buffer").replace(
"The function that executes the augmented Lagrangian SGD with variance reduction:",
"The function that executes the augmented Lagrangian SGD:",
)
)
def al_sgd(
params: List[Tensor],
local_weights: List[Tensor],
dual_weights: List[Tensor],
d_p_list: List[Tensor],
momentum_buffer_list: List[Optional[Tensor]],
weight_decay: float,
momentum: float,
lr: float,
dampening: float,
nesterov: bool,
mu: float,
) -> None:
return al_vr_sgd(
params,
local_weights,
dual_weights,
None,
d_p_list,
momentum_buffer_list,
weight_decay=weight_decay,
momentum=momentum,
lr=lr,
dampening=dampening,
nesterov=nesterov,
mu=mu,
)
def mac_sgd(
params: List[Tensor],
local_weights: List[Tensor],
variance_buffer: List[Tensor],
d_p_list: List[Tensor],
momentum_buffer_list: List[Optional[Tensor]],
*,
weight_decay: float,
momentum: float,
lr: float,
dampening: float,
nesterov: bool,
lam: float,
) -> None:
r"""The function that executes the maximizing correlation (Mac) SGD (with variance reduction)
.. math::
\DeclareMathOperator*{\argmin}{arg\,min}
\argmin_x \{f(x) - \lambda \langle x, x_0 \rangle\}
Parameters
----------
params: list of dict or Parameter,
the parameters to optimize
local_weights: iterable of Parameter,
the (init) local weights,
i.e. the term `x_0` in
.. math::
\lambda \langle x, x_0 \rangle
variance_buffer: list of Parameter, optional,
the variance buffers of the parameters,
used for variance reduction
d_p_list: list of Tensor,
the list of gradients of the parameters
momentum_buffer_list: list of Tensor or list of None,
the list of momentum buffers,
works only if `momentum` > 0
gradient_variance_buffer_list: list of Tensor or list of None,
the list of gradient variance buffers,
works only is `vr` is True
weight_decay: float,
weight decay (L2 penalty)
momentum: float,
momentum factor
lr: float, default 1e-3,
the learning rate
dampening: float,
dampening for momentum
nesterov: bool,
if True, enables Nesterov momentum
lam: float,
the (penalty) coeff. of the maximizing correlation term,
i.e. the term `\lambda` in
.. math::
\lambda \langle x, x_0 \rangle
"""
if local_weights is None:
local_weights = [None] * len(params)
if variance_buffer is None:
variance_buffer = [None] * len(params)
for idx, (param, lw, vb) in enumerate(zip(params, local_weights, variance_buffer)):
d_p = d_p_list[idx]
if weight_decay != 0:
d_p = d_p.add(param, alpha=weight_decay) # L2 regularization
if lam != 0 and lw is not None:
d_p = d_p.sub(lw.detach().clone(), alpha=lam) # maximizing correlation term
if momentum != 0:
buf = momentum_buffer_list[idx]
if buf is None:
buf = torch.clone(d_p).detach()
momentum_buffer_list[idx] = buf
else:
buf.mul_(momentum).add_(d_p, alpha=1 - dampening)
if nesterov:
d_p = d_p.add(buf, alpha=momentum)
else:
d_p = buf
if vb is not None:
d_p = d_p.sub(vb.detach().clone()) # variance reduction
param.add_(d_p, alpha=-lr)