-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
optimizers.py
195 lines (149 loc) · 5.09 KB
/
optimizers.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
import math
from dezero import cuda, Parameter
# =============================================================================
# Optimizer (base class)
# =============================================================================
class Optimizer:
def __init__(self):
self.target = None
self.hooks = []
def setup(self, target):
self.target = target
return self
def update(self):
params = [p for p in self.target.params() if p.grad is not None]
for f in self.hooks:
f(params)
for param in params:
self.update_one(param)
def update_one(self, param):
raise NotImplementedError()
def add_hook(self, f):
self.hooks.append(f)
# =============================================================================
# Hook functions
# =============================================================================
class WeightDecay:
def __init__(self, rate):
self.rate = rate
def __call__(self, params):
for param in params:
param.grad.data += self.rate * param.data
class ClipGrad:
def __init__(self, max_norm):
self.max_norm = max_norm
def __call__(self, params):
total_norm = 0
for param in params:
total_norm += (param.grad.data ** 2).sum()
total_norm = math.sqrt(float(total_norm))
rate = self.max_norm / (total_norm + 1e-6)
if rate < 1:
for param in params:
param.grad.data *= rate
class FreezeParam:
def __init__(self, *layers):
self.freeze_params = []
for l in layers:
if isinstance(l, Parameter):
self.freeze_params.append(l)
else:
for p in l.params():
self.freeze_params.append(p)
def __call__(self, params):
for p in self.freeze_params:
p.grad = None
# =============================================================================
# SGD / MomentumSGD / AdaGrad / AdaDelta / Adam
# =============================================================================
class SGD(Optimizer):
def __init__(self, lr=0.01):
super().__init__()
self.lr = lr
def update_one(self, param):
param.data -= self.lr * param.grad.data
class MomentumSGD(Optimizer):
def __init__(self, lr=0.01, momentum=0.9):
super().__init__()
self.lr = lr
self.momentum = momentum
self.vs = {}
def update_one(self, param):
v_key = id(param)
if v_key not in self.vs:
xp = cuda.get_array_module(param.data)
self.vs[v_key] = xp.zeros_like(param.data)
v = self.vs[v_key]
v *= self.momentum
v -= self.lr * param.grad.data
param.data += v
class AdaGrad(Optimizer):
def __init__(self, lr=0.001, eps=1e-8):
super().__init__()
self.lr = lr
self.eps = eps
self.hs = {}
def update_one(self, param):
xp = cuda.get_array_module(param.data)
h_key = id(param)
if h_key not in self.hs:
self.hs[h_key] = xp.zeros_like(param.data)
lr = self.lr
eps = self.eps
grad = param.grad.data
h = self.hs[h_key]
h += grad * grad
param.data -= lr * grad / (xp.sqrt(h) + eps)
class AdaDelta(Optimizer):
def __init__(self, rho=0.95, eps=1e-6):
super().__init__()
self.rho = rho
self.eps = eps
self.msg = {}
self.msdx = {}
def update_one(self, param):
xp = cuda.get_array_module(param.data)
key = id(param)
if key not in self.msg:
self.msg[key] = xp.zeros_like(param.data)
self.msdx[key] = xp.zeros_like(param.data)
msg, msdx = self.msg[key], self.msdx[key]
rho = self.rho
eps = self.eps
grad = param.grad.data
msg *= rho
msg += (1 - rho) * grad * grad
dx = xp.sqrt((msdx + eps) / (msg + eps)) * grad
msdx *= rho
msdx += (1 - rho) * dx * dx
param.data -= dx
class Adam(Optimizer):
def __init__(self, alpha=0.001, beta1=0.9, beta2=0.999, eps=1e-8):
super().__init__()
self.t = 0
self.alpha = alpha
self.beta1 = beta1
self.beta2 = beta2
self.eps = eps
self.ms = {}
self.vs = {}
def update(self, *args, **kwargs):
self.t += 1
super().update(*args, **kwargs)
@property
def lr(self):
fix1 = 1. - math.pow(self.beta1, self.t)
fix2 = 1. - math.pow(self.beta2, self.t)
return self.alpha * math.sqrt(fix2) / fix1
def update_one(self, param):
xp = cuda.get_array_module(param.data)
key = id(param)
if key not in self.ms:
self.ms[key] = xp.zeros_like(param.data)
self.vs[key] = xp.zeros_like(param.data)
m, v = self.ms[key], self.vs[key]
beta1, beta2, eps = self.beta1, self.beta2, self.eps
grad = param.grad.data
m += (1 - beta1) * (grad - m)
v += (1 - beta2) * (grad * grad - v)
param.data -= self.lr * m / (xp.sqrt(v) + eps)