-
Notifications
You must be signed in to change notification settings - Fork 35
/
theano_nets.py
419 lines (368 loc) · 15.6 KB
/
theano_nets.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import numpy as np
import theano
from theano import tensor as T
import h5py
"""
This script implements necessary functions and layers for
constructing recurrent neural networks.
"""
""" Global variable setting """
_DTYPE = theano.config.floatX
_EPSILON = 1e-8
""" Initializers """
def init_choices():
return ['orthogonal', 'glorot_uniform', 'uniform', 'normal']
""" Saxe et al. Exact solutions to the nonlinear dynamics of learning in deep linear neural networks. 2013. """
def orthogonal(n_dim=None):
assert n_dim is not None
w = np.random.randn(n_dim, n_dim)
u,_,_ = np.linalg.svd(w)
return u.astype(_DTYPE)
""" Glorot and Bengio. Understanding the difficulties of training deep feedforward neural networks. 2010. """
def glorot_uniform(n_in=None, n_out=None):
assert n_in is not None
if n_out is None: n_out = n_in
w = np.random.uniform(
low=-np.sqrt(6. / (n_in + n_out)),
high=np.sqrt(6. / (n_in + n_out)),
size=(n_in, n_out)
)
return w.astype(_DTYPE)
def uniform(n_in=None, n_out=None):
assert n_in is not None
if n_out is None: n_out = n_in
w = np.random.uniform(
low=-1,
high=1,
size=(n_in, n_out)
)
return w.astype(_DTYPE) * 0.02
def normal(n_in=None, n_out=None):
assert n_in is not None
if n_out is None: n_out = n_in
w = np.random.randn(n_in, n_out) * 0.01
return w.astype(_DTYPE)
""" Shortcut functions """
def tanh(x):
return T.tanh(x)
def softmax(x):
if x.ndim == 2:
return T.nnet.softmax(x)
elif x.ndim == 3:
exp_x = T.exp(x)
sum_exp_x = exp_x.sum(2)[:,:,None]
return exp_x / sum_exp_x
else:
raise ValueError('softmax only accepts input dimension of either 2 or 3')
def sigmoid(x):
return T.nnet.sigmoid(x)
def relu(x):
return T.nnet.relu(x)
def linear(x):
return x
""" Regularizers """
def reg_choices():
return ['L2', 'L1', 'L1L2']
def L2(params):
reg_term = 0
for p in params:
# only apply to weight but not bias
if p.ndim > 1:
reg_term += T.sum(p**2)
return reg_term
def L1(params):
reg_term = 0
for p in params:
if p.ndim > 1:
reg_term += T.sum(T.abs_(p))
return reg_term
def L1L2(params):
reg_term = 0
for p in params:
if p.ndim > 1:
reg_term += T.sum(p**2) + T.sum(T.abs_(p))
return reg_term
""" Optimization algorithms """
def optim_choices():
return ['rmsprop', 'adam', 'sgd']
def rmsprop(params, grads, learn_rate):
#learn_rate = theano.shared(np.asarray(learn_rate, dtype=_DTYPE))
rho = theano.shared(np.asarray(0.9, dtype=_DTYPE))
epsilon = theano.shared(np.asarray(1e-8, dtype=_DTYPE))
updates = []
shapes = [p.get_value().shape for p in params]
accums = [theano.shared(np.zeros(shape, dtype=_DTYPE)) for shape in shapes]
for p,g,a in zip(params, grads, accums):
new_a = rho * a + (1. - rho) * T.sqr(g)
new_p = p - learn_rate * g / (T.sqrt(new_a) + epsilon)
updates.append((a, new_a))
updates.append((p, new_p))
return updates
""" Kingma and Ba. Adam: a method for stochastic optimization. In ICLR, 2014. """
def adam(params, grads, learn_rate):
#learn_rate = theano.shared(np.asarray(learn_rate, dtype=_DTYPE))
epsilon = theano.shared(np.asarray(1e-8, dtype=_DTYPE))
beta1 = theano.shared(np.asarray(0.9, dtype=_DTYPE))
beta2 = theano.shared(np.asarray(0.999, dtype=_DTYPE))
t = theano.shared(np.asarray(1, dtype=_DTYPE))
updates = []
shapes = [p.get_value().shape for p in params]
ms = [theano.shared(np.zeros(shape, dtype=_DTYPE)) for shape in shapes]
vs = [theano.shared(np.zeros(shape, dtype=_DTYPE)) for shape in shapes]
alpha = learn_rate * T.sqrt(1 - beta2**t) / (1 - beta1**t)
for p,g,m,v in zip(params, grads, ms, vs):
new_m = beta1 * m + (1 - beta1) * g
new_v = beta2 * v + (1 - beta2) * (g**2)
new_p = p - alpha * new_m / (T.sqrt(new_v) + epsilon)
updates.append((m, new_m))
updates.append((v, new_v))
updates.append((p, new_p))
updates.append((t, t+1))
return updates
def sgd(params, grads, learn_rate):
#learn_rate = theano.shared(np.asarray(learn_rate, dtype=_DTYPE))
momentum = theano.shared(np.asarray(0.9, dtype=_DTYPE))
updates = []
shapes = [p.get_value().shape for p in params]
vs = [theano.shared(np.zeros(shape, dtype=_DTYPE)) for shape in shapes]
for p,g,v in zip(params, grads, vs):
new_v = momentum * v + learn_rate * g
new_p = p - new_v
updates.append((v, new_v))
updates.append((p, new_p))
return updates
""" Layers
Note: minibatch training is not considered here due to reward computation """
def Dropout(state_below, trng, dropout_factor):
return T.switch(dropout_factor,
state_below *
trng.binomial(state_below.shape, p=0.5, n=1, dtype=state_below.dtype),
state_below * 0.5)
class FC(object):
def __init__(self,
state_below=None,
input_dim=None,
output_dim=None,
activation='sigmoid',
W_init='uniform',
use_bias=True,
layer_name='fc',
model_file=None
):
self.state_below = state_below
self.n_in = input_dim
self.n_out = output_dim
self.activation = activation
self.W_init = W_init
self.use_bias = use_bias
self.layer_name = layer_name
self.model_file = model_file
self.build()
self.output = self.step(self.state_below)
def build(self):
self.params = []
if self.model_file is None:
self.W = theano.shared(eval(self.W_init)(self.n_in, self.n_out), name=self.layer_name + '_W')
self.params += [self.W]
if self.use_bias:
self.b = theano.shared(np.zeros((self.n_out), dtype=_DTYPE), name=self.layer_name + '_b')
self.params += [self.b]
else:
with h5py.File(self.model_file, 'r') as saved_model:
self.W = theano.shared(saved_model[self.layer_name + '_W'][...].astype(_DTYPE), name=self.layer_name + '_W')
self.params += [self.W]
if self.use_bias:
self.b = theano.shared(saved_model[self.layer_name + '_b'][...].astype(_DTYPE), name=self.layer_name + '_b')
self.params += [self.b]
def step(self, x):
yraw = T.dot(x, self.W)
if self.use_bias:
yraw += self.b
y = eval(self.activation)(yraw)
return y
def reset_params(self):
self.W.set_value(eval(self.W_init)(self.n_in, self.n_out))
if self.use_bias:
self.b.set_value(np.zeros((self.n_out), dtype=_DTYPE))
def load_params(self, model_file=None):
assert model_file is not None
with h5py.File(model_file, 'r') as saved_model:
self.W.set_value(saved_model[self.layer_name + '_W'][...].astype(_DTYPE))
if self.use_bias:
self.b.set_value(saved_model[self.layer_name + '_b'][...].astype(_DTYPE))
class GRU(object):
def __init__(self,
state_below=None,
input_dim=None,
output_dim=None,
W_init='normal',
U_init='normal',
init_state=None,
go_backwards=False,
layer_name='gru',
model_file=None
):
self.state_below = state_below
self.n_in = input_dim
self.n_out = output_dim
self.W_init = W_init
self.U_init = U_init
self.init_state = init_state
self.go_backwards = go_backwards
self.layer_name = layer_name
self.model_file = model_file
self.build()
self.output = self.step(self.state_below)
def build(self):
if self.model_file is None:
self.W = theano.shared(np.concatenate([eval(self.W_init)(self.n_in, self.n_out),
eval(self.W_init)(self.n_in, self.n_out),
eval(self.W_init)(self.n_in, self.n_out)], axis=1), name=self.layer_name + '_W')
self.U = theano.shared(np.concatenate([eval(self.U_init)(self.n_out),
eval(self.U_init)(self.n_out),
eval(self.U_init)(self.n_out)], axis=1), name=self.layer_name + '_U')
self.b = theano.shared(np.zeros((3 * self.n_out), dtype=_DTYPE), name=self.layer_name + '_b')
else:
with h5py.File(self.model_file, 'r') as saved_model:
self.W = theano.shared(saved_model[self.layer_name + '_W'][...].astype(_DTYPE), name=self.layer_name + '_W')
self.U = theano.shared(saved_model[self.layer_name + '_U'][...].astype(_DTYPE), name=self.layer_name + '_U')
self.b = theano.shared(saved_model[self.layer_name + '_b'][...].astype(_DTYPE), name=self.layer_name + '_b')
self.params = [self.W, self.U, self.b]
def step(self, x):
if self.init_state == None:
init_state = T.alloc(0., self.n_out)
else:
init_state = self.init_state
def _slice(_x, n, dim):
return _x[n*dim:(n+1)*dim]
def _recurrence(_x, _h):
matrix_r = _slice(_x, 0, self.n_out)
matrix_z = _slice(_x, 1, self.n_out)
matrix_h = _slice(_x, 2, self.n_out)
inner_r = T.dot(_h, self.U[:,:self.n_out])
inner_z = T.dot(_h, self.U[:,self.n_out:2*self.n_out])
r = sigmoid(matrix_r + inner_r)
z = sigmoid(matrix_z + inner_z)
inner_h = T.dot(r*_h, self.U[:,2*self.n_out:])
h_p = tanh(matrix_h + inner_h)
h = (1 - z) * _h + z * h_p
return h
x = T.dot(x, self.W) + self.b
rval,_ = theano.scan(
fn=_recurrence,
sequences=x,
outputs_info=init_state,
go_backwards=self.go_backwards
)
if self.go_backwards:
rval = rval[::-1,...]
return rval
def reset_params(self):
self.W.set_value(np.concatenate([eval(self.W_init)(self.n_in, self.n_out),
eval(self.W_init)(self.n_in, self.n_out),
eval(self.W_init)(self.n_in, self.n_out)], axis=1))
self.U.set_value(np.concatenate([eval(self.U_init)(self.n_out),
eval(self.U_init)(self.n_out),
eval(self.U_init)(self.n_out)], axis=1))
self.b.set_value(np.zeros((3 * self.n_out), dtype=_DTYPE))
def load_params(self, model_file=None):
assert model_file is not None
with h5py.File(model_file, 'r') as saved_model:
self.W.set_value(saved_model[self.layer_name + '_W'][...].astype(_DTYPE))
self.U.set_value(saved_model[self.layer_name + '_U'][...].astype(_DTYPE))
self.b.set_value(saved_model[self.layer_name + '_b'][...].astype(_DTYPE))
class LSTM(object):
def __init__(self,
state_below=None,
input_dim=None,
output_dim=None,
W_init='glorot_uniform',
U_init='orthogonal',
init_state=None,
init_memory=None,
go_backwards=False,
layer_name='lstm',
model_file=None
):
self.state_below = state_below
self.n_in = input_dim
self.n_out = output_dim
self.W_init = W_init
self.U_init = U_init
self.init_state = init_state
self.init_memory = init_memory
self.go_backwards = go_backwards
self.layer_name = layer_name
self.model_file = model_file
self.build()
self.output = self.step(self.state_below)
def build(self):
if self.model_file is None:
self.W = theano.shared(np.concatenate([eval(self.W_init)(self.n_in, self.n_out),
eval(self.W_init)(self.n_in, self.n_out),
eval(self.W_init)(self.n_in, self.n_out),
eval(self.W_init)(self.n_in, self.n_out)], axis=1), name=self.layer_name + '_W')
self.U = theano.shared(np.concatenate([eval(self.U_init)(self.n_out),
eval(self.U_init)(self.n_out),
eval(self.U_init)(self.n_out),
eval(self.U_init)(self.n_out)], axis=1), name=self.layer_name + '_U')
self.b = theano.shared(np.zeros((4 * self.n_out), dtype=_DTYPE), name=self.layer_name + '_b')
else:
with h5py.File(self.model_file, 'r') as saved_model:
self.W = theano.shared(saved_model[self.layer_name + '_W'][...].astype(_DTYPE), name=self.layer_name + '_W')
self.U = theano.shared(saved_model[self.layer_name + '_U'][...].astype(_DTYPE), name=self.layer_name + '_U')
self.b = theano.shared(saved_model[self.layer_name + '_b'][...].astype(_DTYPE), name=self.layer_name + '_b')
self.params = [self.W, self.U, self.b]
def step(self, x):
if self.init_state == None:
init_state = T.alloc(0., self.n_out)
else:
init_state = self.init_state
if self.init_memory == None:
init_memory = T.alloc(0., self.n_out)
else:
init_memory = self.init_memory
def _slice(_x, n, dim):
return _x[n*dim:(n+1)*dim]
def _recurrence(_x, _h, _c):
preact = T.dot(_h, self.U)
preact += _x
i = _slice(preact, 0, self.n_out)
f = _slice(preact, 1, self.n_out)
o = _slice(preact, 2, self.n_out)
g = _slice(preact, 3, self.n_out)
i = sigmoid(i)
f = sigmoid(f)
o = sigmoid(o)
g = tanh(g)
c = f * _c + i * g
h = o * tanh(c)
rval = [h, c]
return rval
x = T.dot(x, self.W) + self.b
rval,_ = theano.scan(
fn=_recurrence,
sequences=x,
outputs_info=[init_state, init_memory],
go_backwards=self.go_backwards
)
h = rval[0]
if self.go_backwards:
h = h[::-1,...]
return h
def reset_params(self):
self.W.set_value(np.concatenate([eval(self.W_init)(self.n_in, self.n_out),
eval(self.W_init)(self.n_in, self.n_out),
eval(self.W_init)(self.n_in, self.n_out),
eval(self.W_init)(self.n_in, self.n_out)], axis=1))
self.U.set_value(np.concatenate([eval(self.U_init)(self.n_out),
eval(self.U_init)(self.n_out),
eval(self.U_init)(self.n_out),
eval(self.U_init)(self.n_out)], axis=1))
self.b.set_value(np.zeros((4 * self.n_out), dtype=_DTYPE))
def load_params(self, model_file=None):
assert model_file is not None
with h5py.File(model_file, 'r') as saved_model:
self.W.set_value(saved_model[self.layer_name + '_W'][...].astype(_DTYPE))
self.U.set_value(saved_model[self.layer_name + '_U'][...].astype(_DTYPE))
self.b.set_value(saved_model[self.layer_name + '_b'][...].astype(_DTYPE))