-
Notifications
You must be signed in to change notification settings - Fork 31
/
gating.py
281 lines (250 loc) · 12.4 KB
/
gating.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
"""
Gated recurrent units (Long Short-Term Memory)
Code provided by Mohammad Pezeshki - Nov. 2014 - Universite de Montreal
This code is distributed without any warranty, express or implied.
"""
import numpy as np
import theano
import theano.tensor as T
class LSTM(object):
def __init__(self, n_u, n_h):
self.n_u = int(n_u)
self.n_h = int(n_h)
# Here we define 8 weight matrices and 4 bias vector.
# To determine the dimension of weight matrices and bias vectors,
# we just need to have the following numbers: n_u, n_h
# (You can check it using the equations).
#
# Thus:
# <var>: <dimention>
#
# W_xi : n_h x n_u
# W_hi : n_h x n_h
# W_ci : n_h x n_h
# W_xf : n_h x n_u
# W_hf : n_h x n_h
# W_cf : n_h x n_h
# W_xc : n_h x n_u
# W_hc : n_h x n_h
#
# b_i : n_h x 1 #Note: do not confuse b_i as the bias of input! It's the bias of input GATE
# b_f : n_h x 1
# b_o : n_h x 1
# b_c : n_h x 1
# Input gate weights
self.W_xi = theano.shared(value = np.asarray(
np.random.uniform(
size = (n_h, n_u),
low = -.01, high = .01),
dtype = theano.config.floatX),
name = 'W_xi')
self.W_hi = theano.shared(value = np.asarray(
np.random.uniform(
size = (n_h, n_h),
low = -.01, high = .01),
dtype = theano.config.floatX),
name = 'W_hi')
self.W_ci = theano.shared(value = np.asarray(
np.random.uniform(
size = (n_h, n_h),
low = -.01, high = .01),
dtype = theano.config.floatX),
name = 'W_ci')
# Forget gate weights
self.W_xf = theano.shared(value = np.asarray(
np.random.uniform(
size = (n_h, n_u),
low = -.01, high = .01),
dtype = theano.config.floatX),
name = 'W_xf')
self.W_hf = theano.shared(value = np.asarray(
np.random.uniform(
size = (n_h, n_h),
low = -.01, high = .01),
dtype = theano.config.floatX),
name = 'W_hf')
self.W_cf = theano.shared(value = np.asarray(
np.random.uniform(
size = (n_h, n_h),
low = -.01, high = .01),
dtype = theano.config.floatX),
name = 'W_cf')
# Output gate weights
self.W_xo = theano.shared(value = np.asarray(
np.random.uniform(
size = (n_h, n_u),
low = -.01, high = .01),
dtype = theano.config.floatX),
name = 'W_xo')
self.W_ho = theano.shared(value = np.asarray(
np.random.uniform(
size = (n_h, n_h),
low = -.01, high = .01),
dtype = theano.config.floatX),
name = 'W_ho')
self.W_co = theano.shared(value = np.asarray(
np.random.uniform(
size = (n_h, n_h),
low = -.01, high = .01),
dtype = theano.config.floatX),
name = 'W_co')
# Cell weights
self.W_xc = theano.shared(value = np.asarray(
np.random.uniform(
size = (n_h, n_u),
low = -.01, high = .01),
dtype = theano.config.floatX),
name = 'W_xc')
self.W_hc = theano.shared(value = np.asarray(
np.random.uniform(
size = (n_h, n_h),
low = -.01, high = .01),
dtype = theano.config.floatX),
name = 'W_hc')
# Input gate bias
self.b_i = theano.shared(value = np.zeros(
(n_h, ),
dtype = theano.config.floatX),
name = 'b_i')
# Forget gate bias
self.b_f = theano.shared(value = np.zeros(
(n_h, ),
dtype = theano.config.floatX),
name = 'b_f')
# Output gate bias
self.b_o = theano.shared(value = np.zeros(
(n_h, ),
dtype = theano.config.floatX),
name = 'b_o')
# cell bias
self.b_c = theano.shared(value = np.zeros(
(n_h, ),
dtype = theano.config.floatX),
name = 'b_c')
self.params = [self.W_xi, self.W_hi, self.W_ci,
self.W_xf, self.W_hf, self.W_cf,
self.W_xo, self.W_ho, self.W_co,
self.W_xc, self.W_hc,
self.b_i, self.b_f, self.b_o,
self.b_c]
def lstm_as_activation_function(self, x_t, h_tm1, c_tm1):
#print self.W_xi.get_value(borrow = True)
i_t = T.nnet.sigmoid(T.dot(self.W_xi, x_t) + \
T.dot(self.W_hi, h_tm1) + \
T.dot(self.W_ci, c_tm1) + \
self.b_i)
f_t = T.nnet.sigmoid(T.dot(self.W_xf, x_t) + \
T.dot(self.W_hf, h_tm1) + \
T.dot(self.W_cf, c_tm1) + \
self.b_f)
c_t = f_t * c_tm1 + i_t * \
T.tanh(T.dot(self.W_xc, x_t) + \
T.dot(self.W_hc, h_tm1) + \
self.b_c)
o_t = T.nnet.sigmoid(T.dot(self.W_xo, x_t) + \
T.dot(self.W_ho, h_tm1) + \
T.dot(self.W_co, c_t) + \
self.b_o)
h_t = o_t * T.tanh(c_t)
return h_t, c_t
# Gated Recurrent Unit
# Recently proposed:
# K. Cho, B. van Merrienboer,
# D. Bahdanau, and Y. Bengio.
# On the properties of neural
# machine translation: Encoder-decoder approaches.
class GRU(object):
def __init__(self, n_u, n_h):
self.n_u = int(n_u)
self.n_h = int(n_h)
# Here we define 6 weight matrices and 3 bias vector.
# To determine the dimention of weight matrices and bias vectors,
# we just need to have the following numbers: n_u, n_h
# (You can check it using the equations).
#
# Thus:
# <var>: <dimention>
#
# W_xz : n_h x n_u
# W_hz : n_h x n_h
# W_xr : n_h x n_u
# W_hr : n_h x n_h
# W_xh : n_h x n_h
# W_hh : n_h x n_h
#
# b_z : n_h x 1
# b_r : n_h x 1
# b_h : n_h x 1
# Update gate weights
self.W_xz = theano.shared(value = np.asarray(
np.random.uniform(
size = (n_h, n_u),
low = -.01, high = .01),
dtype = theano.config.floatX),
name = 'W_xz')
self.W_hz = theano.shared(value = np.asarray(
np.random.uniform(
size = (n_h, n_h),
low = -.01, high = .01),
dtype = theano.config.floatX),
name = 'W_hz')
# Reset gate weights
self.W_xr = theano.shared(value = np.asarray(
np.random.uniform(
size = (n_h, n_u),
low = -.01, high = .01),
dtype = theano.config.floatX),
name = 'W_xr')
self.W_hr = theano.shared(value = np.asarray(
np.random.uniform(
size = (n_h, n_h),
low = -.01, high = .01),
dtype = theano.config.floatX),
name = 'W_hr')
# Other weights :-)
self.W_xh = theano.shared(value = np.asarray(
np.random.uniform(
size = (n_h, n_u),
low = -.01, high = .01),
dtype = theano.config.floatX),
name = 'W_xh')
self.W_hh = theano.shared(value = np.asarray(
np.random.uniform(
size = (n_h, n_h),
low = -.01, high = .01),
dtype = theano.config.floatX),
name = 'W_hh')
# Update gate bias
self.b_z = theano.shared(value = np.zeros(
(n_h, ),
dtype = theano.config.floatX),
name = 'b_z')
# Reset gate bias
self.b_r = theano.shared(value = np.zeros(
(n_h, ),
dtype = theano.config.floatX),
name = 'b_r')
# Hidden layer bias
self.b_h = theano.shared(value = np.zeros(
(n_h, ),
dtype = theano.config.floatX),
name = 'b_h')
self.params = [self.W_xz, self.W_hz, self.W_xr, self.W_hr,
self.W_xh, self.W_hh, self.b_z, self.b_r,
self.b_h]
def gru_as_activation_function(self, x_t, h_tm1):
# update gate
z_t = T.nnet.sigmoid(T.dot(self.W_xz, x_t) + \
T.dot(self.W_hz, h_tm1) + \
self.b_z)
# reset gate
r_t = T.nnet.sigmoid(T.dot(self.W_xr, x_t) + \
T.dot(self.W_hr, h_tm1) + \
self.b_r)
# candidate h_t
can_h_t = T.tanh(T.dot(self.W_xh, x_t) + \
r_t * T.dot(self.W_hh, h_tm1) + \
self.b_h)
# h_t
h_t = (1 - z_t) * h_tm1 + z_t * can_h_t
return h_t