-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_model.lua
435 lines (359 loc) · 12.7 KB
/
load_model.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
432
433
434
435
--[[
Setup/load a model using different backends.
List of available models+backends:
(backend: nngraph)
- rnn_vanilla
- lstm_vanilla
(backend: cudnn)
- rnnrelu_cudnn
- rnntanh_cudnn
- lstm_cudnn
- blstm_cudnn
- gru_cudnn
(backend: rnn (Element-Research))
- rnn_rnn
- lstm_rnn
- fastlstm_rnn
- gru_rnn
(backend: rnnlib (facebook))
- rnn_rnnlib
- lstm_rnnlib
- gru_rnnlib
]]
require 'nn'
require 'nngraph'
paths.dofile('modules/RNN.lua')
paths.dofile('modules/LSTM.lua')
------------------------------------------------------------------------------------------------------------
local function error_msg_model()
error('Invalid model name/type: ' .. opt.model)
end
------------------------------------------------------------------------------------------------------------
--[[ Define the criterion used for optimization ]]--
local function setup_criterion()
return nn.CrossEntropyCriterion()
end
--------------------------------------------------------------------------------
-- Vanilla modules
--------------------------------------------------------------------------------
-- ref: https://github.com/jcjohnson/torch-rnn/blob/master/LanguageModel.lua
local function setup_model_vanilla(vocab_size, opt)
assert(vocab_size)
assert(opt, 'Missing input arg: options')
local lookup = nn.LookupTable(vocab_size, opt.rnn_size)
local rnns = {}
local view1 = nn.View(1, 1, -1):setNumInputDims(3) -- flattens the input tensor before feeding to the decoder
local lin = nn.Linear(opt.rnn_size, vocab_size)
-- set network
local model = nn.Sequential()
model:add(lookup)
if opt.dropout > 0 then
model:add(nn.Dropout(opt.dropout))
end
for i=1, opt.num_layers do
-- add module to the network
local rnn
local str = string.lower(opt.model)
if str == 'rnn_vanilla' then
rnn = nn.VanillaRNN(opt.rnn_size, opt.rnn_size)
rnn.remember_states = true
elseif str == 'lstm_vanilla' then
rnn = nn.VanillaLSTM(opt.rnn_size, opt.rnn_size)
rnn.remember_states = true
else
error_msg_model()
end
model:add(rnn)
if opt.dropout > 0 then
model:add(nn.Dropout(opt.dropout))
end
table.insert(rnns, rnn)
end
model:add(view1)
model:add(lin)
model.view1 = view1
model.rnns = rnns
-- monkey patch the forward function to reshape
-- the view modules before doing the forward pass
function model:forward(input)
local N, T = input:size(1), input:size(2)
self.view1:resetSize(N * T, -1)
return self:updateOutput(input)
end
function model:resetStates()
for i, rnn in ipairs(self.rnns) do
rnn:resetStates()
end
end
-- set criterion
local criterion = setup_criterion()
return model, criterion
end
--------------------------------------------------------------------------------
-- RNN lib (Element-Research)
--------------------------------------------------------------------------------
-- ref: https://github.com/Element-Research/rnn/blob/master/examples/recurrent-language-model.lua
local function setup_model_rnn(vocab_size, opt)
assert(vocab_size)
assert(opt, 'Missing input arg: options')
require 'rnn'
local lookup = nn.LookupTable(vocab_size, opt.rnn_size)
lookup.maxOutNorm = -1 -- prevent weird maxnormout behaviour
local lin = nn.Linear(opt.rnn_size, vocab_size)
local model = nn.Sequential()
model:add(lookup)
if opt.dropout > 0 then
model:add(nn.Dropout(opt.dropout))
end
model:add(nn.SplitTable(1))
local stepmodule = nn.Sequential()
for i=1, opt.num_layers do
local rnn
local str = string.lower(opt.model)
if str == 'rnn_rnn' then
local rm = nn.Sequential() -- input is {x[t], h[t-1]}
:add(nn.ParallelTable()
:add(i==1 and nn.Identity() or nn.Linear(opt.rnn_size, opt.rnn_size)) -- input layer
:add(nn.Linear(opt.rnn_size, opt.rnn_size))) -- recurrent layer
:add(nn.CAddTable()) -- merge
:add(nn.Sigmoid()) -- transfer
rnn = nn.Recurrence(rm, opt.rnn_size, 1)
elseif str == 'lstm_rnn' then
rnn = nn.LSTM(opt.rnn_size, opt.rnn_size)
elseif str == 'fastlstm_rnn' then
nn.FastLSTM.usenngraph = true -- faster
nn.FastLSTM.bn = opt.bn
rnn = nn.FastLSTM(opt.rnn_size, opt.rnn_size)
elseif str == 'gru_rnn' then
rnn = nn.GRU(opt.rnn_size, opt.rnn_size)
else
error_msg_model()
end
stepmodule:add(rnn)
if opt.dropout > 0 then
stepmodule:add(nn.Dropout(opt.dropout))
end
end
-- output layer
stepmodule:add(lin)
-- encapsulate stepmodule into a Sequencer
model:add(nn.Sequencer(stepmodule))
-- remember previous state between batches
model:remember((opt.model == 'rnn_rnn' and 'eval') or 'both')
function model:resetStates()
-- do nothing
end
-- set criterion
local criterion = nn.SequencerCriterion(setup_criterion())
return model, criterion
end
--------------------------------------------------------------------------------
-- RNN lib (facebook)
--------------------------------------------------------------------------------
-- ref: https://github.com/facebookresearch/torch-rnnlib/blob/master/examples/word-language-model/word_lm.lua
local function setup_model_rnnlib(vocab_size, opt)
assert(vocab_size)
assert(opt, 'Missing input arg: options')
local rnnlib = require 'rnnlib'
local mutils = require 'rnnlib.mutils'
local use_cudnn = true
local rnn, cellfun, cellstr
local str = string.lower(opt.model)
if str == 'rnn_rnnlib' then
cellfun = rnnlib.cell.RNNTanh
cellstr = 'RNN'
elseif str == 'lstm_rnnlib' then
cellfun = rnnlib.cell.LSTM
cellstr = 'LSTM'
elseif str == 'gru_rnnlib' then
cellfun = rnnlib.cell.GRU
cellstr = 'GRU'
else
error_msg_model()
end
local rnn = nn[cellstr]{
inputsize = opt.rnn_size,
hidsize = opt.rnn_size,
nlayer = opt.num_layers,
usecudnn = use_cudnn,
}
---- setup rnn layers
--local rnn, cellfun, cellstr
--local str = string.lower(opt.model)
--if str == 'rnn_rnnlib' then
-- cellfun = rnnlib.cell.RNNTanh
-- cellstr = 'RNNTanh'
--elseif str == 'lstm_rnnlib' then
-- cellfun = rnnlib.cell.LSTM
-- cellstr = 'LSTM'
--elseif str == 'gru_rnnlib' then
-- cellfun = rnnlib.cell.GRU
-- cellstr = 'GRU'
--else
-- error_msg_model()
--end
--
--if use_cudnn then
-- rnn = rnnlib.makeCudnnRecurrent{
-- cellstring = cellstr,
-- inputsize = opt.inputsize,
-- hids = opt.rnn_size,
-- }
--else
-- rnn = rnnlib.makeRecurrent{
-- cellfn = cellfun,
-- inputsize = opt.inputsize,
-- hids = opt.rnn_size,
-- }
--end
-- Reset the hidden state.
rnn:initializeHidden(opt.batchSize)
local lut = nn.LookupTable(vocab_size, opt.rnn_size)
if opt.uniform then
lut.weight:uniform(-opt.uniform, opt.uniform)
end
local decoder = nn.Linear(opt.rnn_size, vocab_size)
decoder.bias:fill(0)
if opt.uniform then
decoder.weight:uniform(-opt.uniform, opt.uniform)
end
-- set network
local model = nn.Sequential()
model:add(mutils.batchedinmodule(rnn, lut))
-- Select the output of the RNN.
-- The RNN's forward gives a table of { hiddens, outputs }.
model:add(nn.SelectTable(2))
-- Select the output of the last layer, since the output
-- of all layers are returned.
model:add(nn.SelectTable(-1))
-- Flatten the output from bptt x bsz x ntoken to bptt * bsz x ntoken.
-- Note that the first dimension is actually a table, so there is
-- copying involved during the flattening.
model:add(nn.JoinTable(1))
model:add(decoder)
model.rnn = rnn
-- Unroll the rnns.
if not use_cudnn then
for i = 1, #rnn.modules do
rnn.modules[i]:extend(opt.seq_length)
end
end
function model:resetStates()
-- do nothing
end
-- monkey-patch the forward method to only need to accept
-- an input (avoid using the rnn.hiddenbuffer as input)
function model:forward(input)
return self:updateOutput{ rnn.hiddenbuffer, input }
end
-- same monkey-patching for the backward pass
function model:backward(input, gradOutput, scale)
scale = scale or 1
self:updateGradInput({ rnn.hiddenbuffer, input }, gradOutput)
self:accGradParameters({ rnn.hiddenbuffer, input }, gradOutput, scale)
return self.gradInput
end
-- set criterion
local criterion = setup_criterion()
return model, criterion
end
--------------------------------------------------------------------------------
-- CUDNN backend
--------------------------------------------------------------------------------
local function setup_model_cudnn(vocab_size, opt)
assert(vocab_size)
assert(opt, 'Missing input arg: options')
require 'cutorch'
require 'cunn'
require 'cudnn'
local lookup = nn.LookupTable(vocab_size, opt.rnn_size)
local rnns = {}
local view1 = nn.View(1, 1, -1):setNumInputDims(3) -- flattens the input tensor before feeding to the decoder
local lin = nn.Linear(opt.rnn_size, vocab_size)
-- set network
local model = nn.Sequential()
model:add(lookup)
if opt.dropout > 0 then
model:add(nn.Dropout(opt.dropout))
end
model:add(nn.Contiguous())
local rnn
local str = string.lower(opt.model)
if str == 'rnnrelu_cudnn' then
rnn = cudnn.RNNReLU(opt.rnn_size, opt.rnn_size, opt.num_layers, true, opt.dropout, true)
rnn:resetDropoutDescriptor()
elseif str == 'rnntanh_cudnn' then
rnn = cudnn.RNNTanh(opt.rnn_size, opt.rnn_size, opt.num_layers, true, opt.dropout, true)
rnn:resetDropoutDescriptor()
elseif str == 'lstm_cudnn' then
rnn = cudnn.LSTM(opt.rnn_size, opt.rnn_size, opt.num_layers, true, opt.dropout, true)
rnn:resetDropoutDescriptor()
elseif str == 'blstm_cudnn' then
rnn = cudnn.BLSTM(opt.rnn_size, opt.rnn_size, opt.num_layers, true, opt.dropout, true)
rnn:resetDropoutDescriptor()
elseif str == 'gru_cudnn' then
rnn = cudnn.GRU(opt.rnn_size, opt.rnn_size, opt.num_layers, true, opt.dropout, true)
rnn:resetDropoutDescriptor()
else
error_msg_model()
end
model:add(rnn)
model:add(nn.Contiguous())
model:add(view1)
model:add(lin)
model.view1 = view1
model.rnns = rnn
-- monkey patch the forward function to reshape
-- the view modules before doing the forward pass
function model:forward(input)
local N, T = input:size(1), input:size(2)
self.view1:resetSize(N * T, -1)
return self:updateOutput(input)
end
function model:resetStates()
for i, rnn in ipairs(self.rnns) do
rnn:resetStates()
end
end
-- set criterion
local criterion = setup_criterion()
return model, criterion
end
--------------------------------------------------------------------------------
-- Network setup
--------------------------------------------------------------------------------
local function backend_type(opt)
local str = string.lower(opt.model)
if str:find('_cudnn') then
return 'cudnn'
elseif str:find('_rnnlib') then
return 'rnnlib'
elseif str:find('_rnn') then
return 'rnn'
elseif str:find('_vanilla') then
return 'vanilla'
else
error_msg_model()
end
end
------------------------------------------------------------------------------------------------------------
function load_model_criterion(vocab_size, opt)
assert(vocab_size)
assert(opt)
local model, criterion
-- select model backend
local backend_t = backend_type(opt)
if backend_t == 'vanilla' then
model, criterion = setup_model_vanilla(vocab_size, opt)
elseif backend_t == 'rnn' then
model, criterion = setup_model_rnn(vocab_size, opt)
elseif backend_t == 'rnnlib' then
model, criterion = setup_model_rnnlib(vocab_size, opt)
elseif backend_t == 'cudnn' then
opt.dtype = 'torch.CudaTensor' -- force use of cuda
model, criterion = setup_model_cudnn(vocab_size, opt)
else
error('Invalid backend: ' .. opt.model)
end
return model, criterion, backend_t
end