-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathlstm.lua
431 lines (393 loc) · 16.1 KB
/
lstm.lua
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
420
421
422
423
424
425
426
427
428
429
430
431
-- from Karpathy's neuraltalk2
-- with my extension of 2d lstm from the paper: http://arxiv.org/abs/0705.2011
-- since my focus is on 2d, I will not implement the general nd case.
--------------------------------------------------------------------------------
require 'nn'
require 'nngraph'
--------------------------------------------------------------------------------
-- First some exercises over single layer lstm module
--------------------------------------------------------------------------------
local function lstm(x, prev_c, prev_h)
-- Calculate all four gates in one go
local i2h = nn.Linear(params.rnn_size, 4*params.rnn_size)(x)
local h2h = nn.Linear(params.rnn_size, 4*params.rnn_size)(prev_h)
local gates = nn.CAddTable()({i2h, h2h})
-- Reshape to (batch_size, n_gates, hid_size)
-- Then slize the n_gates dimension, i.e dimension 2
local reshaped_gates = nn.Reshape(4,params.rnn_size)(gates)
local sliced_gates = nn.SplitTable(2)(reshaped_gates)
-- Use select gate to fetch each gate and apply nonlinearity
local in_gate = nn.Sigmoid()(nn.SelectTable(1)(sliced_gates))
local in_transform = nn.Tanh()(nn.SelectTable(2)(sliced_gates))
local forget_gate = nn.Sigmoid()(nn.SelectTable(3)(sliced_gates))
local out_gate = nn.Sigmoid()(nn.SelectTable(4)(sliced_gates))
local next_c = nn.CAddTable()({
nn.CMulTable()({forget_gate, prev_c}),
nn.CMulTable()({in_gate, in_transform})
})
local next_h = nn.CMulTable()({out_gate, nn.Tanh()(next_c)})
return next_c, next_h
end
local function lstm2d(x, prev_c, prev_h)
-- Notes about the 2d case:
---- the computation of input x is still the same as in 1d.
---- prev_c, and prev_h is concat of two states from two dimensions.
---- therefore, prev_c, prev_h is twice larger than 1d case.
local sliced_c = nn.SplitTable(2)(prev_c)
local prev_c_1 = nn.SelectTable(1)(sliced_c)
local prev_c_2 = nn.SelectTable(2)(sliced_c)
-- Calculate all four gates in one go
---- for in_gate, in_transform, out_gate, computation is the same excepte
---- we have more input dimensions.
---- for forget_gate, since it is included in the recurrent computations,
---- we will have 2 such gate, each for one recurrent connection.
local i2h = nn.Linear(params.rnn_size, (3+2*1)*params.rnn_size)(x)
local h2h = nn.Linear(d*params.rnn_size, (3+2*1)*params.rnn_size)(prev_h)
local gates = nn.CAddTable()({i2h, h2h})
-- Reshape to (batch_size, n_gates, hid_size)
-- Then slize the n_gates dimension, i.e dimension 2
local reshaped_gates = nn.Reshape((3+2*1),params.rnn_size)(gates)
local sliced_gates = nn.SplitTable(2)(reshaped_gates)
-- Use select gate to fetch each gate and apply nonlinearity
local in_gate = nn.Sigmoid()(nn.SelectTable(1)(sliced_gates))
local in_transform = nn.Tanh()(nn.SelectTable(2)(sliced_gates))
-- two forget_gate
local forget_gate_1 = nn.Sigmoid()(nn.SelectTable(3)(sliced_gates))
local forget_gate_2 = nn.Sigmoid()(nn.SelectTable(4)(sliced_gates))
local out_gate = nn.Sigmoid()(nn.SelectTable(-1)(sliced_gates))
-- we will have only one next state
local next_c = nn.CAddTable()({
nn.CMulTable()({forget_gate_1, prev_c_1}),
nn.CMulTable()({forget_gate_2, prev_c_2}),
nn.CMulTable()({in_gate, in_transform})
})
local next_h = nn.CMulTable()({out_gate, nn.Tanh()(next_c)})
return next_c, next_h
end
--------------------------------------------------------------------------------
-- Now, begin a stacked lstm network
-- Building a computation graph, and returns a module of the inputs and outputs
--------------------------------------------------------------------------------
local LSTM = {}
function LSTM.lstm(input_size, output_size, rnn_size, n, dropout, mult_in)
-- based on the original 1d implementation from Zaremba and Karpathy
-- slightly modified network with more connections from input to deeper layers
-- and from middle layers to final outputs, paper: http://arxiv.org/abs/1308.0850
dropout = dropout or 0
-- there will be 2*n+1 inputs
local inputs = {}
table.insert(inputs, nn.Identity()()) -- indices giving the sequence of symbols
for L = 1,n do
table.insert(inputs, nn.Identity()()) -- prev_c[L]
table.insert(inputs, nn.Identity()()) -- prev_h[L]
end
local x, input_size_L
local outputs = {}
for L = 1,n do
-- c,h from the lower layer
local prev_h = inputs[L*2+1]
local prev_c = inputs[L*2]
-- the input to this layer
if L == 1 then
x = inputs[1]
input_size_L = input_size
else
if mult_in then
x = nn.JoinTable(2)({inputs[1], outputs[(L-1)*2]})
input_size_L = input_size + rnn_size
else
x = outputs[(L-1)*2]
input_size_L = rnn_size
end
if dropout > 0 then x = nn.Dropout(dropout)(x):annotate{name='drop_' .. L} end -- apply dropout, if any
end
-- evaluate the input sums at once for efficiency
local i2h = nn.Linear(input_size_L, 4 * rnn_size)(x):annotate{name='i2h_'..L}
local h2h = nn.Linear(rnn_size, 4 * rnn_size)(prev_h):annotate{name='h2h_'..L}
local all_input_sums = nn.CAddTable()({i2h, h2h})
local reshaped = nn.Reshape(4, rnn_size)(all_input_sums)
local n1, n2, n3, n4 = nn.SplitTable(2)(reshaped):split(4)
-- decode the gates
local in_gate = nn.Sigmoid()(n1)
local forget_gate = nn.Sigmoid()(n2)
local out_gate = nn.Sigmoid()(n3)
-- decode the write inputs
local in_transform = nn.Tanh()(n4)
-- perform the LSTM update
local next_c = nn.CAddTable()({
nn.CMulTable()({forget_gate, prev_c}),
nn.CMulTable()({in_gate, in_transform})
})
-- gated cells form the output
local next_h = nn.CMulTable()({out_gate, nn.Tanh()(next_c)})
table.insert(outputs, next_c)
table.insert(outputs, next_h)
end
local top_size
local top_h = outputs[#outputs]
if mult_in then
top_size = n * rnn_size
for L = n-1,1,-1 do
top_h = nn.JoinTable(2)({outputs[2*L], top_h})
end
else
top_size = rnn_size
end
-- set up the decoder
if dropout > 0 then top_h = nn.Dropout(dropout)(top_h):annotate{name='drop_final'} end
local proj = nn.Linear(top_size, output_size)(top_h):annotate{name='encoder'}
-- local logsoft = nn.LogSoftMax()(proj)
table.insert(outputs, proj)
return nn.gModule(inputs, outputs)
end
function LSTM.lstm2d(input_size, output_size, rnn_size, n, dropout, mult_in)
-- NOTES about 2d case:
-- We only extent the lstm2d along the depth dimension, we haven't touched any
-- core problem of connecting frames in a 2d grid along time.
dropout = dropout or 0
-- there will be 4*n+1 inputs
local inputs = {}
table.insert(inputs, nn.Identity()()) -- indices giving the sequence of symbols
for L = 1,n do
table.insert(inputs, nn.Identity()()) -- prev_layer_1_c[L]
table.insert(inputs, nn.Identity()()) -- prev_layer_1_h[L]
table.insert(inputs, nn.Identity()()) -- prev_layer_2_c[n+L]
table.insert(inputs, nn.Identity()()) -- prev_layer_2_h[n+L]
end
local x, input_size_L
local outputs = {}
for L = 1,n do
-- c,h from the previous 2 layers
local prev_c_1 = inputs[L*2]
local prev_h_1 = inputs[L*2+1]
local prev_c_2 = inputs[(n+L)*2]
local prev_h_2 = inputs[(n+L)*2+1]
local prev_h = nn.JoinTable(2)({prev_h_1, prev_h_2})
-- the input to this layer
if L == 1 then
x = inputs[1]
input_size_L = input_size
else
if mult_in then
x = nn.JoinTable(2)({inputs[1], outputs[(L-1)*2]})
input_size_L = input_size + rnn_size
else
x = outputs[(L-1)*2]
input_size_L = rnn_size
end
if dropout > 0 then x = nn.Dropout(dropout)(x):annotate{name='drop_' .. L} end -- apply dropout, if any
end
-- evaluate the input sums at once for efficiency
local i2h = nn.Linear(input_size_L, (3+2*1) * rnn_size)(x):annotate{name='i2h_'..L}
local h2h = nn.Linear(2*rnn_size, (3+2*1) * rnn_size)(prev_h):annotate{name='h2h_'..L}
local all_input_sums = nn.CAddTable()({i2h, h2h})
local reshaped = nn.Reshape(5, rnn_size)(all_input_sums)
local n1, n2, n3, n4, n5 = nn.SplitTable(2)(reshaped):split(5)
-- decode the gates
local in_gate = nn.Sigmoid()(n1)
local forget_gate_1 = nn.Sigmoid()(n2)
local forget_gate_2 = nn.Sigmoid()(n3)
local out_gate = nn.Sigmoid()(n4)
-- decode the write inputs
local in_transform = nn.Tanh()(n5)
-- perform the LSTM update
local next_c = nn.CAddTable()({
nn.CMulTable()({forget_gate_1, prev_c_1}),
nn.CMulTable()({forget_gate_2, prev_c_2}),
nn.CMulTable()({in_gate, in_transform})
})
-- gated cells form the output
local next_h = nn.CMulTable()({out_gate, nn.Tanh()(next_c)})
table.insert(outputs, next_c)
table.insert(outputs, next_h)
end
local top_size
local top_h = outputs[#outputs]
if mult_in then
top_size = n * rnn_size
for L = n-1,1,-1 do
top_h = nn.JoinTable(2)({outputs[2*L], top_h})
end
else
top_size = rnn_size
end
-- set up the encoder
if dropout > 0 then top_h = nn.Dropout(dropout)(top_h):annotate{name='drop_final'} end
local proj = nn.Linear(top_size, output_size)(top_h):annotate{name='encoder'}
-- local logsoft = nn.LogSoftMax()(proj)
table.insert(outputs, proj)
return nn.gModule(inputs, outputs)
end
function LSTM.lstm3d(input_size, output_size, rnn_size, n, dropout, mult_in)
-- extension of 2d case with more than 2 pixel neighbors:
dropout = dropout or 0
-- there will be 6*n+1 inputs
local inputs = {}
table.insert(inputs, nn.Identity()()) -- indices giving the sequence of symbols
for L = 1,n do
table.insert(inputs, nn.Identity()()) -- prev_layer_1_c[L]
table.insert(inputs, nn.Identity()()) -- prev_layer_1_h[L]
table.insert(inputs, nn.Identity()()) -- prev_layer_2_c[n+L]
table.insert(inputs, nn.Identity()()) -- prev_layer_2_h[n+L]
table.insert(inputs, nn.Identity()()) -- prev_layer_3_c[2n+L]
table.insert(inputs, nn.Identity()()) -- prev_layer_3_h[2n+L]
end
local x, input_size_L
local outputs = {}
for L = 1,n do
-- c,h from the previous 2 layers
local prev_c_1 = inputs[L*2]
local prev_h_1 = inputs[L*2+1]
local prev_c_2 = inputs[(n+L)*2]
local prev_h_2 = inputs[(n+L)*2+1]
local prev_c_3 = inputs[(2*n+L)*2]
local prev_h_3 = inputs[(2*n+L)*2+1]
local prev_h = nn.JoinTable(2)({prev_h_1, prev_h_2, prev_h_3})
-- the input to this layer
if L == 1 then
x = inputs[1]
input_size_L = input_size
else
if mult_in then
x = nn.JoinTable(2)({inputs[1], outputs[(L-1)*2]})
input_size_L = input_size + rnn_size
else
x = outputs[(L-1)*2]
input_size_L = rnn_size
end
if dropout > 0 then x = nn.Dropout(dropout)(x):annotate{name='drop_' .. L} end -- apply dropout, if any
end
-- evaluate the input sums at once for efficiency, here we have 3 forget gates
local i2h = nn.Linear(input_size_L, (3+3*1) * rnn_size)(x):annotate{name='i2h_'..L}
local h2h = nn.Linear(3*rnn_size, (3+3*1) * rnn_size)(prev_h):annotate{name='h2h_'..L}
local all_input_sums = nn.CAddTable()({i2h, h2h})
local reshaped = nn.Reshape(6, rnn_size)(all_input_sums)
local n1, n2, n3, n4, n5, n6 = nn.SplitTable(2)(reshaped):split(6)
-- decode the gates
local in_gate = nn.Sigmoid()(n1)
local forget_gate_1 = nn.Sigmoid()(n2)
local forget_gate_2 = nn.Sigmoid()(n3)
local forget_gate_3 = nn.Sigmoid()(n4)
local out_gate = nn.Sigmoid()(n5)
-- decode the write inputs
local in_transform = nn.Tanh()(n6)
-- perform the LSTM update
local next_c = nn.CAddTable()({
nn.CMulTable()({forget_gate_1, prev_c_1}),
nn.CMulTable()({forget_gate_2, prev_c_2}),
nn.CMulTable()({forget_gate_3, prev_c_3}),
nn.CMulTable()({in_gate, in_transform})
})
-- gated cells form the output
local next_h = nn.CMulTable()({out_gate, nn.Tanh()(next_c)})
table.insert(outputs, next_c)
table.insert(outputs, next_h)
end
local top_size
local top_h = outputs[#outputs]
if mult_in then
top_size = n * rnn_size
for L = n-1,1,-1 do
top_h = nn.JoinTable(2)({outputs[2*L], top_h})
end
else
top_size = rnn_size
end
-- set up the encoder
if dropout > 0 then top_h = nn.Dropout(dropout)(top_h):annotate{name='drop_final'} end
local proj = nn.Linear(top_size, output_size)(top_h):annotate{name='encoder'}
-- local logsoft = nn.LogSoftMax()(proj)
table.insert(outputs, proj)
return nn.gModule(inputs, outputs)
end
function LSTM.lstm4d(input_size, output_size, rnn_size, n, dropout, mult_in)
-- extension of 2d case with more than 2 pixel neighbors:
dropout = dropout or 0
-- there will be 8*n+1 inputs
local inputs = {}
table.insert(inputs, nn.Identity()()) -- indices giving the sequence of symbols
for L = 1,n do
table.insert(inputs, nn.Identity()()) -- prev_layer_1_c[L]
table.insert(inputs, nn.Identity()()) -- prev_layer_1_h[L]
table.insert(inputs, nn.Identity()()) -- prev_layer_2_c[n+L]
table.insert(inputs, nn.Identity()()) -- prev_layer_2_h[n+L]
table.insert(inputs, nn.Identity()()) -- prev_layer_3_c[2n+L]
table.insert(inputs, nn.Identity()()) -- prev_layer_3_h[2n+L]
table.insert(inputs, nn.Identity()()) -- prev_layer_4_c[3n+L]
table.insert(inputs, nn.Identity()()) -- prev_layer_4_h[3n+L]
end
local x, input_size_L
local outputs = {}
for L = 1,n do
-- c,h from the previous 2 layers
local prev_c_1 = inputs[L*2]
local prev_h_1 = inputs[L*2+1]
local prev_c_2 = inputs[(n+L)*2]
local prev_h_2 = inputs[(n+L)*2+1]
local prev_c_3 = inputs[(2*n+L)*2]
local prev_h_3 = inputs[(2*n+L)*2+1]
local prev_c_4 = inputs[(3*n+L)*2]
local prev_h_4 = inputs[(3*n+L)*2+1]
local prev_h = nn.JoinTable(2)({prev_h_1, prev_h_2, prev_h_3, prev_h_4})
-- the input to this layer
if L == 1 then
x = inputs[1]
input_size_L = input_size
else
if mult_in then
x = nn.JoinTable(2)({inputs[1], outputs[(L-1)*2]})
input_size_L = input_size + rnn_size
else
x = outputs[(L-1)*2]
input_size_L = rnn_size
end
if dropout > 0 then x = nn.Dropout(dropout)(x):annotate{name='drop_' .. L} end -- apply dropout, if any
end
-- evaluate the input sums at once for efficiency, here we have 3 forget gates
local i2h = nn.Linear(input_size_L, (3+4*1) * rnn_size)(x):annotate{name='i2h_'..L}
local h2h = nn.Linear(4*rnn_size, (3+4*1) * rnn_size)(prev_h):annotate{name='h2h_'..L}
local all_input_sums = nn.CAddTable()({i2h, h2h})
local reshaped = nn.Reshape(7, rnn_size)(all_input_sums)
local n1, n2, n3, n4, n5, n6, n7 = nn.SplitTable(2)(reshaped):split(7)
-- decode the gates
local in_gate = nn.Sigmoid()(n1)
local forget_gate_1 = nn.Sigmoid()(n2)
local forget_gate_2 = nn.Sigmoid()(n3)
local forget_gate_3 = nn.Sigmoid()(n4)
local forget_gate_4 = nn.Sigmoid()(n5)
local out_gate = nn.Sigmoid()(n6)
-- decode the write inputs
local in_transform = nn.Tanh()(n7)
-- perform the LSTM update
local next_c = nn.CAddTable()({
nn.CMulTable()({forget_gate_1, prev_c_1}),
nn.CMulTable()({forget_gate_2, prev_c_2}),
nn.CMulTable()({forget_gate_3, prev_c_3}),
nn.CMulTable()({forget_gate_4, prev_c_4}),
nn.CMulTable()({in_gate, in_transform})
})
-- gated cells form the output
local next_h = nn.CMulTable()({out_gate, nn.Tanh()(next_c)})
table.insert(outputs, next_c)
table.insert(outputs, next_h)
end
local top_size
local top_h = outputs[#outputs]
if mult_in then
top_size = n * rnn_size
for L = n-1,1,-1 do
top_h = nn.JoinTable(2)({outputs[2*L], top_h})
end
else
top_size = rnn_size
end
-- set up the encoder
if dropout > 0 then top_h = nn.Dropout(dropout)(top_h):annotate{name='drop_final'} end
local proj = nn.Linear(top_size, output_size)(top_h):annotate{name='encoder'}
-- local logsoft = nn.LogSoftMax()(proj)
table.insert(outputs, proj)
return nn.gModule(inputs, outputs)
end
--net = LSTM.lstm(4,5,10,2,0,true)
--graph.dot(net.fg, 'LSTM', 'test_net_mult2')
return LSTM