-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupdates.py
146 lines (113 loc) · 4.63 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
# This optimization module is taken from Passage: https://github.com/IndicoDataSolutions/Passage/tree/master/passage
import theano
import theano.tensor as T
import numpy as np
def clip_norm(g, c, n):
# c can either be a float value of a theano variable.
'''
isTheanoVariable = hasattr(c,'ndim')
if isTheanoVariable:
if T.gt(c , 0):
g = T.switch(T.ge(n, c), g*c/n, g)
return g
else:
'''
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(g**2) 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:
norms = T.sqrt(T.sum(T.sqr(p), axis=0))
desired = T.clip(norms, 0, maxnorm)
p = p * (desired/ (1e-7 + 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):
p = self.max_norm(p, self.maxnorm)
return p
class Update(object):
def __init__(self, regularizer=Regularizer(), clipnorm=0.):
self.__dict__.update(locals())
def get_updates(self, params, grads):
raise NotImplementedError
class RMSprop(Update):
def __init__(self, lr=0.001, rho=0.9, epsilon=1e-6, *args, **kwargs):
Update.__init__(self, *args, **kwargs)
self.__dict__.update(locals())
def get_updates(self, params, cost):
updates = []
grads_unclipped = T.grad(cost, params)
grads = clip_norms(grads_unclipped, self.clipnorm)
for p,g in zip(params,grads):
g = self.regularizer.gradient_regularize(p, g)
acc = theano.shared(p.get_value() * 0.)
acc_new = self.rho * acc + (1 - self.rho) * g ** 2
updates.append((acc, acc_new))
updated_p = p - self.lr * (g / T.sqrt(acc_new + self.epsilon))
updated_p = self.regularizer.weight_regularize(updated_p)
updates.append((p, updated_p))
return updates,grads_unclipped
class Adagrad(Update):
def __init__(self, lr=0.01, epsilon=1e-6, *args, **kwargs):
Update.__init__(self, *args, **kwargs)
self.__dict__.update(locals())
def get_updates(self, params, cost):
updates = []
grads_unclipped = T.grad(cost, params)
grads = clip_norms(grads_unclipped, 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,grads_unclipped
class Momentum(Update):
def __init__(self, lr=0.01, momentum=0.9, *args, **kwargs):
Update.__init__(self, *args, **kwargs)
self.__dict__.update(locals())
def get_updates(self, params, cost):
updates = []
grads_unclipped = T.grad(cost, params)
grads = clip_norms(grads_unclipped, 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,grads
class Adadelta(Update):
def __init__(self, lr=0.5, rho=0.95, epsilon=1e-6, *args, **kwargs):
Update.__init__(self, *args, **kwargs)
self.__dict__.update(locals())
def get_updates(self, params, cost):
updates = []
grads_unclipped = T.grad(cost, params)
grads = clip_norms(grads_unclipped, 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_new = self.rho * acc + (1 - self.rho) * g ** 2
updates.append((acc,acc_new))
update = g * T.sqrt(acc_delta + self.epsilon) / T.sqrt(acc_new + self.epsilon)
updated_p = p - self.lr * update
updated_p = self.regularizer.weight_regularize(updated_p)
updates.append((p, updated_p))
acc_delta_new = self.rho * acc_delta + (1 - self.rho) * update ** 2
updates.append((acc_delta,acc_delta_new))
return updates,grads_unclipped