-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathlstm.py
257 lines (184 loc) · 7.87 KB
/
lstm.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
import numpy as np
import theano
import theano.tensor as T
from lib import sigmoid, softmax, dropout, floatX, random_weights, zeros
class NNLayer:
def get_params(self):
return self.params
def get_param_names(self):
return [ 'UNK' if p.name is None else p.name for p in self.params ]
def save_model(self):
return
def load_model(self):
return
def updates(self):
return []
def reset_state(self):
return
class LSTMLayer(NNLayer):
def __init__(self, num_input, num_cells, input_layers=None, name="", go_backwards=False):
"""
LSTM Layer
Takes as input sequence of inputs, returns sequence of outputs
"""
self.name = name
self.num_input = num_input
self.num_cells = num_cells
if len(input_layers) >= 2:
self.X = T.concatenate([input_layer.output() for input_layer in input_layers], axis=1)
else:
self.X = input_layers[0].output()
self.h0 = theano.shared(floatX(np.zeros(num_cells)))
self.s0 = theano.shared(floatX(np.zeros(num_cells)))
self.go_backwards = go_backwards
self.W_gx = random_weights((num_input, num_cells), name=self.name+"W_gx")
self.W_ix = random_weights((num_input, num_cells), name=self.name+"W_ix")
self.W_fx = random_weights((num_input, num_cells), name=self.name+"W_fx")
self.W_ox = random_weights((num_input, num_cells), name=self.name+"W_ox")
self.W_gh = random_weights((num_cells, num_cells), name=self.name+"W_gh")
self.W_ih = random_weights((num_cells, num_cells), name=self.name+"W_ih")
self.W_fh = random_weights((num_cells, num_cells), name=self.name+"W_fh")
self.W_oh = random_weights((num_cells, num_cells), name=self.name+"W_oh")
self.b_g = zeros(num_cells, name=self.name+"b_g")
self.b_i = zeros(num_cells, name=self.name+"b_i")
self.b_f = zeros(num_cells, name=self.name+"b_f")
self.b_o = zeros(num_cells, name=self.name+"b_o")
self.params = [self.W_gx, self.W_ix, self.W_ox, self.W_fx,
self.W_gh, self.W_ih, self.W_oh, self.W_fh,
self.b_g, self.b_i, self.b_f, self.b_o,
]
self.output()
def one_step(self, x, h_tm1, s_tm1):
"""
"""
g = T.tanh(T.dot(x, self.W_gx) + T.dot(h_tm1, self.W_gh) + self.b_g)
i = T.nnet.sigmoid(T.dot(x, self.W_ix) + T.dot(h_tm1, self.W_ih) + self.b_i)
f = T.nnet.sigmoid(T.dot(x, self.W_fx) + T.dot(h_tm1, self.W_fh) + self.b_f)
o = T.nnet.sigmoid(T.dot(x, self.W_ox) + T.dot(h_tm1, self.W_oh) + self.b_o)
s = i*g + s_tm1 * f
h = T.tanh(s) * o
return h, s
def output(self, train=True):
outputs_info = [self.h0, self.s0]
([outputs, states], updates) = theano.scan(
fn=self.one_step,
sequences=self.X,
outputs_info = outputs_info,
go_backwards = self.go_backwards
)
return outputs
def reset_state(self):
self.h0 = theano.shared(floatX(np.zeros(self.num_cells)))
self.s0 = theano.shared(floatX(np.zeros(self.num_cells)))
class GRULayer(NNLayer):
def __init__(self, num_input, num_cells, input_layers=None, name="", go_backwards=False):
"""
GRU Layer
Takes as input sequence of inputs, returns sequence of outputs
"""
self.name = name
self.num_input = num_input
self.num_cells = num_cells
if len(input_layers) >= 2:
self.X = T.concatenate([input_layer.output() for input_layer in input_layers], axis=1)
else:
self.X = input_layers[0].output()
self.s0 = zeros(num_cells)
self.go_backwards = go_backwards
self.U_z = random_weights((num_input, num_cells), name=self.name+"U_z")
self.W_z = random_weights((num_cells, num_cells), name=self.name+"W_z")
self.U_r = random_weights((num_input, num_cells), name=self.name+"U_r")
self.W_r = random_weights((num_cells, num_cells), name=self.name+"W_r")
self.U_h = random_weights((num_input, num_cells), name=self.name+"U_h")
self.W_h = random_weights((num_cells, num_cells), name=self.name+"W_h")
self.b_z = zeros(num_cells, name=self.name+"b_z")
self.b_r = zeros(num_cells, name=self.name+"b_r")
self.b_h = zeros(num_cells, name=self.name+"b_h")
self.params = [ self.U_z, self.W_z, self.U_r,
self.W_r, self.U_h, self.W_h,
self.b_z, self.b_r, self.b_h
]
self.output()
def one_step(self, x, s_tm1):
"""
"""
z = T.nnet.sigmoid(T.dot(x, self.U_z) + T.dot(s_tm1, self.W_z) + self.b_z)
r = T.nnet.sigmoid(T.dot(x, self.U_r) + T.dot(s_tm1, self.W_r) + self.b_r)
h = T.tanh(T.dot(x, self.U_h) + T.dot(s_tm1 * r, self.W_h) + self.b_h)
s = (1-z) * h + z * s_tm1
return [s]
def output(self, train=True):
outputs_info = [self.s0]
(outputs, updates) = theano.scan(
fn=self.one_step,
sequences=self.X,
outputs_info = outputs_info,
go_backwards = self.go_backwards
)
return outputs
def reset_state(self):
self.s0 = zeros(self.num_cells)
class FullyConnectedLayer(NNLayer):
"""
"""
def __init__(self, num_input, num_output, input_layers, name=""):
if len(input_layers) >= 2:
self.X = T.concatenate([input_layer.output() for input_layer in input_layers], axis=1)
else:
self.X = input_layers[0].output()
self.W_yh = random_weights((num_input, num_output),name="W_yh")
self.b_y = zeros(num_output, name="b_y")
self.params = [self.W_yh, self.b_y]
def output(self):
return T.dot(self.X, self.W_yh) + self.b_y
class InputLayer(NNLayer):
"""
"""
def __init__(self, X, name=""):
self.name = name
self.X = X
self.params=[]
def output(self, train=False):
return self.X
class SoftmaxLayer(NNLayer):
"""
"""
def __init__(self, num_input, num_output, input_layer, temperature=1.0, name=""):
self.name = ""
self.X = input_layer
self.params = []
self.temp = temperature
self.W_yh = random_weights((num_input, num_output),name="W_yh")
self.b_y = zeros(num_output, name="b_y")
self.params = [self.W_yh, self.b_y]
def output(self, train=True):
if train:
input_sequence = self.X.output(train=True)
else:
input_sequence = self.X.output(train=False)
return softmax((T.dot(input_sequence, self.W_yh) + self.b_y), temperature=self.temp)
class SigmoidLayer(NNLayer):
def __init__(self, num_input, num_output, input_layers, name=""):
if len(input_layers) >= 2:
print "number of input layers: %s" % len(input_layers)
print "len of list comprehension: %s" % len([input_layer.output() for input_layer in input_layers])
self.X = T.concatenate([input_layer.output() for input_layer in input_layers], axis=1)
else:
self.X = input_layers[0].output()
self.W_yh = random_weights((num_input, num_output),name="W_yh")
self.b_y = zeros(num_output, name="b_y")
self.params = [self.W_yh, self.b_y]
def output(self):
return sigmoid(T.dot(self.X, self.W_yh) + self.b_y)
class DropoutLayer(NNLayer):
def __init__(self, input_layer, name="", dropout_probability=0.):
self.X = input_layer.output()
self.params = []
self.dropout_probability = dropout_probability
def output(self):
return dropout(self.X, self.dropout_probability)
class MergeLayer(NNLayer):
def init(self, input_layers):
return
def output(self):
return