forked from yoonkim/CNN_sentence
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathupdates.py
309 lines (265 loc) · 10.5 KB
/
updates.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
import theano
import theano.tensor as T
import numpy as np
def clip_norm(g, c, n):
if c > 0:
g = T.switch(T.ge(n, c), g*c/n, g)
return g
def clip_norms(gs, c):
norm = T.sqrt(sum(T.sum(T.sqr(g)) for g in gs))
return [clip_norm(g, c, norm) for g in gs]
class Regularizer(object):
def __init__(self, l1=0., l2=0., maxnorm=0.):
self.__dict__.update(locals())
def max_norm(self, p, maxnorm):
if maxnorm > 0:
col_norms = p.norm(2, axis=0)
desired = T.clip(col_norms, 0, maxnorm)
p *= (desired / (1e-7 + col_norms))
return p
def gradient_regularize(self, p, g):
g += p * self.l2
g += T.sgn(p) * self.l1
return g
def weight_regularize(self, p):
return self.max_norm(p, self.maxnorm)
class UpdateRule(object):
def __init__(self, regularizer=Regularizer(), clipnorm=0.):
self.__dict__.update(locals())
def updates(self, cost, params):
raise NotImplementedError
class SGD(UpdateRule):
"""
Stochastic Gradient Descent (SGD) updates
Generates update expressions of the form:
* ``param := param - learning_rate * gradient``
"""
def __init__(self, lr=0.01, *args, **kwargs):
"""
Parameters
----------
lr : float or symbolic scalar
The learning rate controlling the size of update steps
"""
UpdateRule.__init__(self, *args, **kwargs)
self.__dict__.update(locals())
def updates(self, cost, params):
"""
Parameters
----------
cost : symbolic expression or list of expressions
A scalar loss expression, or a list of gradient expressions
params : list of shared variables
The variables to generate update expressions for
learning_rate : float or symbolic scalar
The learning rate controlling the size of update steps
"""
updates = []
grads = T.grad(cost, params)
grads = clip_norms(grads, self.clipnorm)
for p,g in zip(params,grads):
g = self.regularizer.gradient_regularize(p, g)
updated_p = p - self.lr * g
updated_p = self.regularizer.weight_regularize(updated_p)
updates.append((p, updated_p))
return updates
class Momentum(UpdateRule):
def __init__(self, lr=0.01, momentum=0.9, *args, **kwargs):
UpdateRule.__init__(self, *args, **kwargs)
self.__dict__.update(locals())
def updates(self, cost, params):
"""
Parameters
----------
cost : symbolic expression or list of expressions
A scalar loss expression, or a list of gradient expressions
params : list of shared variables
The variables to generate update expressions for
learning_rate : float or symbolic scalar
The learning rate controlling the size of update steps
"""
updates = []
grads = T.grad(cost, params)
grads = clip_norms(grads, self.clipnorm)
for p,g in zip(params,grads):
g = self.regularizer.gradient_regularize(p, g)
m = theano.shared(p.get_value() * 0.)
v = (self.momentum * m) - (self.lr * g)
updates.append((m, v))
updated_p = p + v
updated_p = self.regularizer.weight_regularize(updated_p)
updates.append((p, updated_p))
return updates
class NAG(UpdateRule):
def __init__(self, lr=0.01, momentum=0.9, *args, **kwargs):
UpdateRule.__init__(self, *args, **kwargs)
self.__dict__.update(locals())
def updates(self, cost, params):
"""
Parameters
----------
cost : symbolic expression or list of expressions
A scalar loss expression, or a list of gradient expressions
params : list of shared variables
The variables to generate update expressions for
learning_rate : float or symbolic scalar
The learning rate controlling the size of update steps
"""
updates = []
grads = T.grad(cost, params)
grads = clip_norms(grads, self.clipnorm)
for p, g in zip(params, grads):
g = self.regularizer.gradient_regularize(p, g)
m = theano.shared(p.get_value() * 0.)
v = (self.momentum * m) - (self.lr * g)
updates.append((m,v))
updated_p = p + self.momentum * v - self.lr * g
updated_p = self.regularizer.weight_regularize(updated_p)
updates.append((p, updated_p))
return updates
class RMSprop(UpdateRule):
"""
References
----------
.. [1] Tieleman, T. and Hinton, G. (2012):
Neural Networks for Machine Learning, Lecture 6.5 - rmsprop.
Coursera. http://www.youtube.com/watch?v=O3sxAc4hxZU (formula @5:20)
"""
def __init__(self, lr=0.001, rho=0.9, epsilon=1e-6, *args, **kwargs):
UpdateRule.__init__(self, *args, **kwargs)
self.__dict__.update(locals())
def updates(self, cost, params):
"""
Parameters
----------
cost : symbolic expression or list of expressions
A scalar loss expression, or a list of gradient expressions
params : list of shared variables
The variables to generate update expressions for
learning_rate : float or symbolic scalar
The learning rate controlling the size of update steps
"""
updates = []
grads = T.grad(cost, params)
grads = clip_norms(grads, self.clipnorm)
for p,g in zip(params,grads):
g = self.regularizer.gradient_regularize(p, g)
acc = theano.shared(p.get_value() * 0.)
acc_t = self.rho * acc + (1 - self.rho) * T.sqr(g)
updates.append((acc, acc_t))
updated_p = p - self.lr * (g / T.sqrt(acc_t + self.epsilon))
updated_p = self.regularizer.weight_regularize(updated_p)
updates.append((p, updated_p))
return updates
class Adam(UpdateRule):
"""
References
----------
.. [1] Kingma, Diederik, and Jimmy Ba (2014):
Adam: A Method for Stochastic Optimization.
arXiv preprint arXiv:1412.6980.
"""
def __init__(self, lr=0.001, b1=0.9, b2=0.999, e=1e-8, *args, **kwargs):
UpdateRule.__init__(self, *args, **kwargs)
self.__dict__.update(locals())
def updates(self, cost, params):
"""
Parameters
----------
cost : symbolic expression or list of expressions
A scalar loss expression, or a list of gradient expressions
params : list of shared variables
The variables to generate update expressions for
learning_rate : float or symbolic scalar
The learning rate controlling the size of update steps
"""
updates = []
grads = T.grad(cost, params)
grads = clip_norms(grads, self.clipnorm)
i = theano.shared(np.asarray(0., dtype=theano.config.floatX))
i_t = i + 1
lr_t = self.lr * (T.sqrt(self.b2 ** i_t) / (self.b1 ** i_t))
for p, g in zip(params, grads):
m = theano.shared(p.get_value() * 0.)
v = theano.shared(p.get_value() * 0.)
m_t = self.b1 * m + (1. - self.b1) * g
v_t = self.b2 * v + (1. - self.b2) * T.sqr(g)
g_t = m_t / (T.sqrt(v_t) + self.e)
g_t = self.regularizer.gradient_regularize(p, g_t)
p_t = p - lr_t * g_t
p_t = self.regularizer.weight_regularize(p_t)
updates.append((m, m_t))
updates.append((v, v_t))
updates.append((p, p_t))
updates.append((i, i_t))
return updates
class Adagrad(UpdateRule):
"""
References
----------
.. [1] Duchi, J., Hazan, E., & Singer, Y. (2011):
Adaptive subgradient methods for online learning and stochastic
optimization. JMLR, 12:2121-2159.
.. [2] Chris Dyer:
Notes on AdaGrad. http://www.ark.cs.cmu.edu/cdyer/adagrad.pdf
"""
def __init__(self, lr=0.01, epsilon=1e-6, *args, **kwargs):
UpdateRule.__init__(self, *args, **kwargs)
self.__dict__.update(locals())
def updates(self, cost, params):
"""
Parameters
----------
cost : symbolic expression or list of expressions
A scalar loss expression, or a list of gradient expressions
params : list of shared variables
The variables to generate update expressions for
learning_rate : float or symbolic scalar
The learning rate controlling the size of update steps
"""
updates = []
grads = T.grad(cost, params)
grads = clip_norms(grads, self.clipnorm)
for p,g in zip(params,grads):
g = self.regularizer.gradient_regularize(p, g)
acc = theano.shared(p.get_value() * 0.)
acc_t = acc + g ** 2
updates.append((acc, acc_t))
p_t = p - (self.lr / T.sqrt(acc_t + self.epsilon)) * g
p_t = self.regularizer.weight_regularize(p_t)
updates.append((p, p_t))
return updates
class Adadelta(UpdateRule):
def __init__(self, rho=0.95, epsilon=1e-6, *args, **kwargs):
"""
learning rate is 1.
"""
UpdateRule.__init__(self, *args, **kwargs)
self.__dict__.update(locals())
def updates(self, cost, params):
"""
Parameters
----------
cost : symbolic expression or list of expressions
A scalar loss expression, or a list of gradient expressions
params : list of shared variables
The variables to generate update expressions for
learning_rate : float or symbolic scalar
The learning rate controlling the size of update steps
"""
updates = []
grads = T.grad(cost, params)
grads = clip_norms(grads, self.clipnorm)
for p,g in zip(params,grads):
g = self.regularizer.gradient_regularize(p, g)
acc = theano.shared(p.get_value() * 0.)
acc_delta = theano.shared(p.get_value() * 0.)
acc_t = self.rho * acc + (1 - self.rho) * T.sqr(g)
updates.append((acc,acc_t))
update = g * T.sqrt(acc_delta + self.epsilon) / T.sqrt(acc_t + self.epsilon)
updated_p = p - update
updated_p = self.regularizer.weight_regularize(updated_p)
updates.append((p, updated_p))
acc_delta_t = self.rho * acc_delta + (1 - self.rho) * T.sqr(update)
updates.append((acc_delta,acc_delta_t))
return updates