forked from zackchase/PyRNN
/
lstm.py
230 lines (166 loc) · 5.73 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
import numpy as np
import theano
import theano.tensor as T
from lib import softmax, dropout, floatX, random_weights, zeros
class NNLayer:
def get_params(self):
return 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_layer=None, name=""):
"""
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
self.X = input_layer.output()
self.h0 = theano.shared(floatX(np.zeros(num_cells)))
self.s0 = theano.shared(floatX(np.zeros(num_cells)))
self.W_gx = random_weights((num_input, num_cells))
self.W_ix = random_weights((num_input, num_cells))
self.W_fx = random_weights((num_input, num_cells))
self.W_ox = random_weights((num_input, num_cells))
self.W_gh = random_weights((num_cells, num_cells))
self.W_ih = random_weights((num_cells, num_cells))
self.W_fh = random_weights((num_cells, num_cells))
self.W_oh = random_weights((num_cells, num_cells))
self.b_g = zeros(num_cells)
self.b_i = zeros(num_cells)
self.b_f = zeros(num_cells)
self.b_o = zeros(num_cells)
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,
]
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):
([outputs, states], updates) = theano.scan(
fn=self.one_step,
sequences=self.X,
outputs_info = [self.h0, self.s0],
)
self.new_s = states[-1]
self.new_h = outputs[-1]
return outputs
def updates(self):
return [(self.s0, self.new_s), (self.h0, self.new_h)]
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 FullyConnectedLayer(NNLayer):
"""
"""
def __init__self(self, num_input, num_output, input_layer, name=""):
self.X = input_layer.output()
self.num_input = num_input
self.num_output = num_output
self.W = random_weights((num_input, num_output))
self.b = zeros(num_output)
self.params = [self.W, self.B]
def output(self):
return
class InputNPLayer(NNLayer):
"""
"""
def __init__(self, X, name=""):
self.name = name
self.params=[]
self.X = X#theano.shared(X)
self.params=[X]
def output(self):
return self.X
class InputPLayer(NNLayer):
"""
"""
def __init__(self, X, Z, name=""):
self.name = name
self.params=[]
self.X = X#theano.shared(X)
self.Z = Z#theano.shared(Z)#dnodex.umatrix[0,:,:]
self.params=[X,Z]
def output(self):
return T.dot(self.X,self.Z)
class InputLayer(NNLayer):
"""
"""
def __init__(self, X, name=""):
self.name = name
self.X = X
self.params=[]
def output(self):
return self.X
class SoftmaxNPLayer(NNLayer):
"""
"""
def __init__(self, num_input, num_output, input_layer, temperature=1.0, name=""):
self.name = ""
self.X = input_layer.output()
self.params = []
self.temp = temperature
self.W_yh = random_weights((num_input, num_output))
self.b_y = zeros(num_output)
self.params = [self.W_yh, self.b_y]
def output(self):
return softmax((T.dot(self.X, self.W_yh) + self.b_y), temperature=self.temp)
class SoftmaxPLayer(NNLayer):
"""
"""
def __init__(self, num_input, num_output, Z, input_layer, temperature=1.0, name=""):
self.name = ""
self.X = input_layer.output()
self.params = []
self.temp = temperature
self.Z=Z
self.W_yh = random_weights((num_input, num_output))
self.b_y = zeros(num_output)
self.params = [self.W_yh, self.b_y]
def output(self):
return softmax((T.dot(T.dot(self.X, self.W_yh) + self.b_y, self.Z)), temperature=self.temp)
class SoftmaxLayer(NNLayer):
"""
"""
def __init__(self, num_input, num_output, input_layer, temperature=1.0, name=""):
self.name = ""
self.X = input_layer.output()
self.params = []
self.temp = temperature
self.W_yh = random_weights((num_input, num_output))
self.b_y = zeros(num_output)
self.params = [self.W_yh, self.b_y]
def output(self):
return softmax((T.dot(self.X, self.W_yh) + self.b_y), temperature=self.temp)
class SigmoidLayer(NNLayer):
def __init__(self, input_layer, name=""):
self.X = input_layer.output()
self.params = []
def output(self):
return sigmoid(self.X)
class DropoutLayer(NNLayer):
def __init__(self, input_layer, name=""):
self.X = input_layer.output()
self.params = []
def output(self):
return dropout(self.X)
class MergeLayer(NNLayer):
def init(self, input_layers):
return
def output(self):
return