-
Notifications
You must be signed in to change notification settings - Fork 17
/
lstm_net_cythonic.py
345 lines (264 loc) · 16.3 KB
/
lstm_net_cythonic.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
#author : Suhas Pillai
import pdb
from lstm_layer import *
class LSTM_net_cythonic:
'''
This class is same as LSTM_net class, only difference is that it calls cython methods, than the normal python methods, reduces training time by 1/3 rd
'''
def __init__(self):
pass
def MDLSTM_lstm_conv_feed_layer_forward(self,X,model,conv_param):
'''
The method is used for forward propagation of MDLSTM and Convolutional subsampling layer.
'''
lstm_layer_obj = Layer()
model_frwd = model['forward']
model_bckd = model['backward']
model_frwd_flip = model['forward_flip']
model_bckd_flip = model['backward_flip']
C,W,H = X.shape
X_frwd = np.zeros((C,W+1,H+1))
X_frwd[:,1:,1:] = X
iter_C,iter_W,iter_H = X_frwd.shape
X_frwd_flip = np.zeros(X_frwd.shape)
for i in xrange(1,iter_W):
X_frwd_flip[:,i,:] = X_frwd[:,iter_W-i,:]
X_bckd = np.zeros(X_frwd.shape)
for i in xrange(1,iter_H):
X_bckd[:,:,i] = X_frwd[:,:,iter_H-i]
W_xi = model_frwd['W_xi']
h_prev_frwd = np.zeros((W_xi.shape[1],X_frwd.shape[1],X_frwd.shape[2]))
h_prev_bckd = h_prev_frwd.copy()
# now for flip image
X_bckd_flip = np.zeros(X_frwd_flip.shape)
C_iter,W_iter,H_iter = X_bckd.shape
for i in xrange(1,iter_H):
X_bckd_flip[:,:,i] = X_frwd_flip[:,:,iter_H-i]
h_prev_frwd_flip = np.zeros((W_xi.shape[1],X_frwd_flip.shape[1],X_frwd_flip.shape[2]))
h_prev_bckd_flip = h_prev_frwd_flip.copy()
#------------------------------------------ Calling cython code----------------------------------------------#
h_frwd,cache_lstm_frwd =lstm_layer_obj .forward_propagation_cythonic(X_frwd, model_frwd,h_prev_frwd)
h_bckd,cache_lstm_bckd =lstm_layer_obj .forward_propagation_cythonic(X_bckd,model_bckd,h_prev_bckd)
h_frwd_flip,cache_lstm_frwd_flip = lstm_layer_obj.forward_propagation_cythonic(X_frwd_flip, model_frwd_flip,h_prev_frwd_flip)
h_bckd_flip,cache_lstm_bckd_flip = lstm_layer_obj.forward_propagation_cythonic(X_bckd_flip,model_bckd_flip,h_prev_bckd_flip)
h_bckd_align = np.zeros(h_bckd.shape)
for i in xrange(1,iter_H):
h_bckd_align[:,:,i] = h_bckd[:,:,iter_H-i]
h_bckd_flip_align = np.zeros(h_bckd_flip.shape)
for i in xrange(1,iter_H):
h_bckd_flip_align[:,:,i] = h_bckd_flip[:,:,iter_H-i]
# now unflip both the images
h_frwd_unflip = np.zeros(h_frwd_flip.shape)
h_bckd_unflip_align = np.zeros(h_bckd_flip_align.shape)
for i in xrange(1,iter_W):
h_frwd_unflip[:,i,:] = h_frwd_flip[:,iter_W-i,:]
h_bckd_unflip_align[:,i,:] = h_bckd_flip_align[:,iter_W-i,:]
# give this to conv layer all as different input
W_conv_frwd = model['conv']['W_conv_frwd']
W_conv_bckd = model['conv']['W_conv_bckd']
W_conv_frwd_flip = model['conv']['W_conv_frwd_flip']
W_conv_bckd_flip = model['conv']['W_conv_bckd_flip']
b_conv_frwd = model['conv']['b_conv_frwd']
b_conv_bckd = model['conv']['b_conv_bckd']
b_conv_frwd_flip = model['conv']['b_conv_frwd_flip']
b_conv_bckd_flip = model['conv']['b_conv_bckd_flip']
#b_conv = model['conv']['b_conv']
C,W,H = h_frwd.shape
h_frwd_new = h_frwd[:,1:,1:].reshape(1,C,W-1,H-1)
h_bckd_align_new = h_bckd_align[:,1:,1:].reshape(1, C,(W-1),(H-1))
h_frwd_unflip_new = h_frwd_unflip[:,1:,1:].reshape(1,C,(W-1),(H-1))
h_bckd_unflip_align_new =h_bckd_unflip_align[:,1:,1:].reshape(1,C,(W-1),(H-1))
# calling normal python code
'''
dout_conv_frwd, cache_conv_frwd = lstm_layer_obj.conv_subsampling_forward_multidim(h_frwd_new,W_conv_frwd, b_conv_frwd, conv_param)
dout_conv_bckd,cache_conv_bckd = lstm_layer_obj.conv_subsampling_forward_multidim( h_bckd_align_new , W_conv_bckd, b_conv_bckd, conv_param)
dout_conv_frwd_flip,cache_conv_frwd_flip = lstm_layer_obj.conv_subsampling_forward_multidim(h_frwd_unflip_new, W_conv_frwd_flip, b_conv_frwd_flip, conv_param)
dout_conv_bckd_flip,cache_conv_bckd_flip = lstm_layer_obj.conv_subsampling_forward_multidim(h_bckd_unflip_align_new , W_conv_bckd_flip, b_conv_bckd_flip, conv_param)
'''
#calling cython code
dout_conv_frwd, cache_conv_frwd = lstm_layer_obj.conv_subsampling_forward_multidim_cython(h_frwd_new,W_conv_frwd, b_conv_frwd, conv_param)
dout_conv_bckd,cache_conv_bckd = lstm_layer_obj.conv_subsampling_forward_multidim_cython( h_bckd_align_new , W_conv_bckd, b_conv_bckd, conv_param)
dout_conv_frwd_flip,cache_conv_frwd_flip = lstm_layer_obj.conv_subsampling_forward_multidim_cython(h_frwd_unflip_new, W_conv_frwd_flip, b_conv_frwd_flip, conv_param)
dout_conv_bckd_flip,cache_conv_bckd_flip = lstm_layer_obj.conv_subsampling_forward_multidim_cython(h_bckd_unflip_align_new , W_conv_bckd_flip, b_conv_bckd_flip, conv_param)
# giving input to feed forward layer, but first need to reshape it in proper format
N_ff, C_ff, W_ff, H_ff = dout_conv_frwd.shape
# summing all convo outputs
out_conv = dout_conv_frwd + dout_conv_bckd + dout_conv_frwd_flip + dout_conv_bckd_flip
out_conv = out_conv.reshape(C_ff,W_ff * H_ff)
out_conv = out_conv.T
cache = (cache_lstm_frwd,cache_lstm_bckd ,cache_lstm_frwd_flip,cache_lstm_bckd_flip,cache_conv_frwd,cache_conv_bckd, \
cache_conv_frwd_flip,cache_conv_bckd_flip,dout_conv_frwd)
return out_conv,cache
def MDLSTM_lstm_conv_feed_layer_backward(self,dscores,cache):
'''
The method is used for backpropagation of MDLSTM and Convolutional subsampling layers.
'''
lstm_layer_obj = Layer()
cache_lstm_frwd,cache_lstm_bckd ,cache_lstm_frwd_flip,cache_lstm_bckd_flip,cache_conv_frwd,cache_conv_bckd, \
cache_conv_frwd_flip,cache_conv_bckd_flip,dout_conv_frwd = cache
N,C,W,H = dout_conv_frwd.shape
dscores = dscores.reshape(N,C,W,H)
dout_ff_frwd_conv = dscores.copy()
dout_ff_bckd_conv = dscores.copy()
dout_ff_frwd_flip_conv = dscores.copy()
dout_ff_bckd_flip_conv = dscores.copy()
#calling normal python code
'''
dx_frwd_conv,dw_frwd_conv,db_frwd_conv = lstm_layer_obj.conv_subsampling_backward_multidim(dout_ff_frwd_conv, cache_conv_frwd)
dx_bckd_conv,dw_bckd_conv,db_bckd_conv = lstm_layer_obj.conv_subsampling_backward_multidim(dout_ff_bckd_conv, cache_conv_bckd)
dx_frwd_flip_conv,dw_frwd_flip_conv,db_frwd_flip_conv = lstm_layer_obj.conv_subsampling_backward_multidim(dout_ff_frwd_flip_conv, cache_conv_frwd_flip)
dx_bckd_flip_conv,dw_bckd_flip_conv,db_bckd_flip_conv = lstm_layer_obj.conv_subsampling_backward_multidim(dout_ff_bckd_flip_conv, cache_conv_bckd_flip)
'''
# calling cython code
dx_frwd_conv,dw_frwd_conv,db_frwd_conv = lstm_layer_obj.conv_subsampling_backward_multidim_cython(dout_ff_frwd_conv, cache_conv_frwd)
dx_bckd_conv,dw_bckd_conv,db_bckd_conv = lstm_layer_obj.conv_subsampling_backward_multidim_cython(dout_ff_bckd_conv, cache_conv_bckd)
dx_frwd_flip_conv,dw_frwd_flip_conv,db_frwd_flip_conv = lstm_layer_obj.conv_subsampling_backward_multidim_cython(dout_ff_frwd_flip_conv, cache_conv_frwd_flip)
dx_bckd_flip_conv,dw_bckd_flip_conv,db_bckd_flip_conv = lstm_layer_obj.conv_subsampling_backward_multidim_cython(dout_ff_bckd_flip_conv, cache_conv_bckd_flip)
# passing through lstm layer
N_conv,C_conv,W_conv,H_conv = dx_frwd_conv.shape
X,model,h,cell_state,arr_i,arr_f_d1,arr_f_d2,arr_o,arr_g = cache_lstm_frwd
dout_frwd_new = np.zeros(h.shape)
dout_frwd = dx_frwd_conv.reshape(C_conv,W_conv,H_conv)
dout_bckd = dx_bckd_conv.reshape(C_conv,W_conv,H_conv)
dout_frwd_new[:,1:,1:] = dout_frwd
iter_C,iter_W,iter_H = dout_frwd_new.shape
dout_bckd_new = np.zeros(h.shape)
C,W,H = dout_bckd.shape
for i in xrange(H):
dout_bckd_new[:,1:,i+1] = dout_bckd[:,:,H-i-1]
# now for flip images
dout_frwd_flip = dx_frwd_flip_conv.reshape(C_conv,W_conv,H_conv)
dout_bckd_flip = dx_bckd_flip_conv.reshape(C_conv,W_conv,H_conv)
dout_frwd_flip_new = np.zeros(h.shape)
dout_frwd_flip_new[:,1:,1:] = dout_frwd_flip
dout_bckd_flip_new = np.zeros(h.shape)
dout_bckd_flip_new[:,1:,1:] = dout_bckd_flip
# flip both
C,W,H = dout_frwd_flip.shape
for i in xrange (W):
dout_frwd_flip_new[:,i+1,1:] = dout_frwd_flip[:,W-i-1,:]
dout_bckd_flip_new[:,i+1,1:] = dout_bckd_flip[:,W-i-1,:]
# align the bckd_flip
iter_C,iter_W,iter_H = dout_bckd_flip_new.shape
dout_bckd_flip_new_align = np.zeros(dout_bckd_flip_new.shape)
for i in xrange(1,iter_H):
dout_bckd_flip_new_align[:,:,i] = dout_bckd_flip_new[:,:,iter_H-i]
dx_frwd,grads_frwd = lstm_layer_obj.backward_propagation_cythonic(dout_frwd_new,cache_lstm_frwd)
dx_bckd, grads_bckd = lstm_layer_obj.backward_propagation_cythonic(dout_bckd_new,cache_lstm_bckd)
dx_frwd_flip,grads_frwd_flip = lstm_layer_obj.backward_propagation_cythonic(dout_frwd_flip_new,cache_lstm_frwd_flip)
dx_bckd_flip, grads_bckd_flip = lstm_layer_obj.backward_propagation_cythonic(dout_bckd_flip_new_align,cache_lstm_bckd_flip)
dx_bckd_realign = np.zeros(dx_bckd.shape)
for i in xrange(1,iter_H):
dx_bckd_realign[:,:,i] = dx_bckd[:,:,iter_H-i]
# now unflip
dx_frwd_unflip = np.zeros(dx_frwd_flip.shape)
dx_bckd_flip_realign = np.zeros(dx_bckd_flip.shape)
for i in xrange(1,iter_H):
dx_bckd_flip_realign[:,:,i] = dx_bckd_flip[:,:,iter_H-i]
dx_bckd_realign_unflip = np.zeros(dx_bckd_flip_realign.shape)
for i in xrange(1,iter_W):
dx_frwd_unflip[:,i,:] = dx_frwd_flip[:,iter_W-i,:]
dx_bckd_realign_unflip[:,i,:] = dx_bckd_flip_realign[:,iter_W-i,:]
# realign bckd_flip
dx = dx_frwd[:,1:,1:] + dx_bckd_realign[:,1:,1:] + dx_frwd_unflip[:,1:,1:] + dx_bckd_realign_unflip[:,1:,1:]
grads_conv = {'W_conv_frwd':dw_frwd_conv,'b_conv_frwd':db_frwd_conv,'W_conv_bckd':dw_bckd_conv,'b_conv_bckd':db_bckd_conv,\
'W_conv_frwd_flip':dw_frwd_flip_conv,'b_conv_frwd_flip':db_frwd_flip_conv,'W_conv_bckd_flip':dw_bckd_flip_conv,'b_conv_bckd_flip':db_bckd_flip_conv}
return dx, grads_conv, grads_frwd, grads_bckd, grads_frwd_flip, grads_bckd_flip
def MDLSTM_lstm_forward_layer(self,X,model):
'''
The method performs Multidimension LSTM forward propagation
'''
lstm_layer_obj = Layer()
model_frwd = model['forward']
model_bckd = model['backward']
model_frwd_flip = model['forward_flip']
model_bckd_flip = model['backward_flip']
C,W,H = X.shape
X_frwd = np.zeros((C,W+1,H+1))
X_frwd[:,1:,1:] = X
iter_C,iter_W,iter_H = X_frwd.shape
X_frwd_flip = np.zeros(X_frwd.shape)
for i in xrange(1,iter_W):
X_frwd_flip[:,i,:] = X_frwd[:,iter_W-i,:]
X_bckd = np.zeros(X_frwd.shape)
for i in xrange(1,iter_H):
X_bckd[:,:,i] = X_frwd[:,:,iter_H-i]
W_xi = model_frwd['W_xi']
h_prev_frwd = np.zeros((W_xi.shape[1],X_frwd.shape[1],X_frwd.shape[2]))
h_prev_bckd = h_prev_frwd.copy()
# now for flip image
X_bckd_flip = np.zeros(X_frwd_flip.shape)
C_iter,W_iter,H_iter = X_bckd.shape
for i in xrange(1,iter_H):
X_bckd_flip[:,:,i] = X_frwd_flip[:,:,iter_H-i]
h_prev_frwd_flip = np.zeros((W_xi.shape[1],X_frwd_flip.shape[1],X_frwd_flip.shape[2]))
h_prev_bckd_flip = h_prev_frwd_flip.copy()
h_frwd,cache_lstm_frwd =lstm_layer_obj .forward_propagation_cythonic(X_frwd, model_frwd,h_prev_frwd)
h_bckd,cache_lstm_bckd =lstm_layer_obj .forward_propagation_cythonic(X_bckd,model_bckd,h_prev_bckd)
h_frwd_flip,cache_lstm_frwd_flip = lstm_layer_obj.forward_propagation_cythonic(X_frwd_flip, model_frwd_flip,h_prev_frwd_flip)
h_bckd_flip,cache_lstm_bckd_flip = lstm_layer_obj.forward_propagation_cythonic(X_bckd_flip,model_bckd_flip,h_prev_bckd_flip)
h_bckd_align = np.zeros(h_bckd.shape)
for i in xrange(1,iter_H):
h_bckd_align[:,:,i] = h_bckd[:,:,iter_H-i]
h_bckd_flip_align = np.zeros(h_bckd_flip.shape)
for i in xrange(1,iter_H):
h_bckd_flip_align[:,:,i] = h_bckd_flip[:,:,iter_H-i]
# now unflip both the images
h_frwd_unflip = np.zeros(h_frwd_flip.shape)
h_bckd_unflip_align = np.zeros(h_bckd_flip_align.shape)
for i in xrange(1,iter_W):
h_frwd_unflip[:,i,:] = h_frwd_flip[:,iter_W-i,:]
h_bckd_unflip_align[:,i,:] = h_bckd_flip_align[:,iter_W-i,:]
cache = (cache_lstm_frwd,cache_lstm_bckd,cache_lstm_frwd_flip,cache_lstm_bckd_flip)
return h_frwd,h_bckd,h_frwd_unflip,h_bckd_unflip_align,cache
def MDLSTM_backward_layer(self,dout_frwd,dout_bckd,dout_frwd_flip,dout_bckd_flip,cache):
'''
The method performs Multidimension LSTM backward propagation
'''
lstm_layer_obj = Layer()
cache_lstm_frwd,cache_lstm_bckd,cache_lstm_frwd_flip,cache_lstm_bckd_flip = cache
X,model,h,cell_state,arr_i,arr_f_d1,arr_f_d2,arr_o,arr_g = cache_lstm_frwd
dout_frwd_new = np.zeros(h.shape)
dout_frwd = dout_frwd.reshape(h.shape[0],h.shape[1]-1,h.shape[2]-1)
dout_bckd = dout_bckd.reshape(h.shape[0],h.shape[1]-1,h.shape[2]-1)
dout_frwd_new[:,1:,1:] = dout_frwd
iter_C,iter_W,iter_H = dout_frwd_new.shape
dout_bckd_new = np.zeros(h.shape)
C,W,H = dout_bckd.shape
for i in xrange(H):
dout_bckd_new[:,1:,i+1] = dout_bckd[:,:,H-i-1]
# now for flip images
dout_frwd_flip = dout_frwd_flip.reshape(h.shape[0],h.shape[1]-1,h.shape[2]-1)
dout_bckd_flip = dout_bckd_flip.reshape(h.shape[0],h.shape[1]-1,h.shape[2]-1)
dout_frwd_flip_new = np.zeros(h.shape)
dout_frwd_flip_new[:,1:,1:] = dout_frwd_flip
dout_bckd_flip_new = np.zeros(h.shape)
dout_bckd_flip_new[:,1:,1:] = dout_bckd_flip
# flip both
C,W,H = dout_frwd_flip.shape
for i in xrange (W):
dout_frwd_flip_new[:,i+1,1:] = dout_frwd_flip[:,W-i-1,:]
dout_bckd_flip_new[:,i+1,1:] = dout_bckd_flip[:,W-i-1,:]
# align the bckd_flip
iter_C,iter_W,iter_H = dout_bckd_flip_new.shape
dout_bckd_flip_new_align = np.zeros(dout_bckd_flip_new.shape)
for i in xrange(1,iter_H):
dout_bckd_flip_new_align[:,:,i] = dout_bckd_flip_new[:,:,iter_H-i]
dx_frwd,grads_frwd = lstm_layer_obj.backward_propagation_cythonic(dout_frwd_new,cache_lstm_frwd)
dx_bckd, grads_bckd = lstm_layer_obj.backward_propagation_cythonic(dout_bckd_new,cache_lstm_bckd)
dx_frwd_flip,grads_frwd_flip = lstm_layer_obj.backward_propagation_cythonic(dout_frwd_flip_new,cache_lstm_frwd_flip)
dx_bckd_flip, grads_bckd_flip = lstm_layer_obj.backward_propagation_cythonic(dout_bckd_flip_new_align,cache_lstm_bckd_flip)
dx_bckd_realign = np.zeros(dx_bckd.shape)
for i in xrange(1,iter_H):
dx_bckd_realign[:,:,i] = dx_bckd[:,:,iter_H-i]
# now unflip
dx_frwd_unflip = np.zeros(dx_frwd_flip.shape)
dx_bckd_flip_realign = np.zeros(dx_bckd_flip.shape)
for i in xrange(1,iter_H):
dx_bckd_flip_realign[:,:,i] = dx_bckd_flip[:,:,iter_H-i]
dx_bckd_realign_unflip = np.zeros(dx_bckd_flip_realign.shape)
for i in xrange(1,iter_W):
dx_frwd_unflip[:,i,:] = dx_frwd_flip[:,iter_W-i,:]
dx_bckd_realign_unflip[:,i,:] = dx_bckd_flip_realign[:,iter_W-i,:]
dx = dx_frwd[:,1:,1:] + dx_bckd_realign[:,1:,1:] + dx_frwd_unflip[:,1:,1:] + dx_bckd_realign_unflip[:,1:,1:]
return dx,grads_frwd,grads_bckd,grads_frwd_flip,grads_bckd_flip