-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfedpd.py
57 lines (46 loc) · 1.36 KB
/
fedpd.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
"""
For original implementation, see `FedPD <https://github.com/564612540/FedPD>`_.
"""
from typing import Iterable, Union
from torch.nn import Parameter
from torch_ecg.utils import add_docstring
from ._register import register_optimizer
from .base import AL_SGD, AL_SGD_VR
__all__ = [
"FedPD_VR",
"FedPD_SGD",
]
@register_optimizer()
class FedPD_VR(AL_SGD_VR):
"""Local optimizer for ``FedPD`` using SGD with variance reduction.
Parameters
----------
params : Iterable[dict] or Iterable[torch.nn.parameter.Parameter]
Model parameters to be optimized.
lr : float, default: 1e-3
Learning rate.
mu : float, default: 1.0
The (penalty) coeff. of the augmented Lagrangian term.
"""
def __init__(
self,
params: Iterable[Union[dict, Parameter]],
lr: float = 1e-3,
mu: float = 1.0,
) -> None:
super().__init__(params, lr=lr, mu=mu, momentum=0)
@register_optimizer()
@add_docstring(
FedPD_VR.__doc__.replace(
"Local optimizer for ``FedPD`` using SGD with variance reduction.",
"Local optimizer for ``FedPD`` using SGD.",
)
)
class FedPD_SGD(AL_SGD):
def __init__(
self,
params: Iterable[Union[dict, Parameter]],
lr: float = 1e-3,
mu: float = 1.0,
) -> None:
super().__init__(params, lr=lr, mu=mu, momentum=0)