forked from yaringal/BayesianRNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_dropout.lua
695 lines (654 loc) · 24.5 KB
/
main_dropout.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
-- Bayesian dropout by extending main_naive_dropout. Setting dropout_h = 0 should recover
-- (approx) the other file. We use the same noise mask throughout the seq, but different masks
-- for different gates. We init sequences with 0 rather than prev state.
-- ToDo:
-- V * fix fp_MC (underflow issues)
-- V * implement embedding dropout
-- V * optimise code (lstm unit is 2-3 times slower now)
-- * text fp_MC fix
-- * run_test_all with MC_dropout=true takes ages
local ok,cunn = pcall(require, 'fbcunn')
if not ok then
ok,cunn = pcall(require,'cunn')
if ok then
print("warning: fbcunn not found. Falling back to cunn")
LookupTable = nn.LookupTable
else
print("Could not find cunn or fbcunn. Either is required")
os.exit()
end
else
deviceParams = cutorch.getDeviceProperties(1)
cudaComputeCapability = deviceParams.major + deviceParams.minor/10
LookupTable = nn.LookupTable
end
require('nngraph')
require('base')
local ptb = require('data')
-- -- param 7:
-- local params = {batch_size=20,
-- seq_length=20,
-- layers=2,
-- decay=1.,
-- rnn_size=200,
-- dropout_i=0.5,
-- dropout_h=0.5,
-- dropout_o=0.5,
-- init_weight=0.1,
-- lr=1,
-- vocab_size=10000,
-- max_epoch=180, -- start decreasing learning rate, keeping lr > 0.004
-- max_max_epoch=180,
-- max_grad_norm=5,
-- MC_dropout=false,
-- T=10}
-- -- debug param 7:
-- local params = {batch_size=20,
-- seq_length=20,
-- layers=2,
-- decay=1.,
-- -- rnn_size=100, -- small enough to run on GPU 2
-- rnn_size=200, -- small enough to run on GPU 2
-- dropout_x=0,
-- dropout_i=0.5,
-- dropout_h=0,
-- dropout_o=0.5,
-- init_weight=0.1,
-- lr=1,
-- vocab_size=10000,
-- max_epoch=180, -- start decreasing learning rate, keeping lr > 0.004
-- max_max_epoch=180,
-- max_grad_norm=5,
-- MC_dropout=false,
-- T=10} -- lots more iterations
-- -- param 12:
-- local params = {batch_size=20,
-- seq_length=20,
-- layers=2,
-- decay=1.,
-- rnn_size=200,
-- dropout_x=0,
-- dropout_i=0.5,
-- dropout_h=0,
-- dropout_o=0.5,
-- init_weight=0.1,
-- lr=1,
-- vocab_size=10000,
-- max_epoch=180, -- start decreasing learning rate, keeping lr > 0.004
-- max_max_epoch=180,
-- max_grad_norm=5,
-- MC_dropout=false,
-- T=10} -- lots more iterations
-- -- param 13:
-- local params = {batch_size=20,
-- seq_length=20,
-- layers=2,
-- decay=1.,
-- rnn_size=200,
-- dropout_x=0.1,
-- dropout_i=0.5,
-- dropout_h=0.1,
-- dropout_o=0.5,
-- init_weight=0.1,
-- lr=1,
-- vocab_size=10000,
-- max_epoch=180, -- start decreasing learning rate, keeping lr > 0.004
-- max_max_epoch=180,
-- max_grad_norm=5,
-- MC_dropout=false,
-- T=10} -- lots more iterations
-- -- param 14:
-- local params = {batch_size=20,
-- seq_length=20,
-- layers=2,
-- decay=1.,
-- rnn_size=200,
-- dropout_x=0.25,
-- dropout_i=0.25,
-- dropout_h=0.25,
-- dropout_o=0.25,
-- init_weight=0.1,
-- lr=1,
-- vocab_size=10000,
-- max_epoch=180, -- start decreasing learning rate, keeping lr > 0.004
-- max_max_epoch=180,
-- max_grad_norm=5,
-- MC_dropout=false,
-- T=10}
-- -- param 15:
-- local params = {batch_size=20,
-- seq_length=20,
-- layers=2,
-- decay=1.,
-- rnn_size=200,
-- dropout_x=0.4,
-- dropout_i=0.4,
-- dropout_h=0.4,
-- dropout_o=0.4,
-- init_weight=0.1,
-- lr=1,
-- vocab_size=10000,
-- max_epoch=180, -- start decreasing learning rate, keeping lr > 0.004
-- max_max_epoch=180,
-- max_grad_norm=5,
-- MC_dropout=false,
-- T=10}
-- -- param 16:
-- local params = {batch_size=20,
-- seq_length=20,
-- layers=2,
-- decay=1.,
-- rnn_size=200,
-- dropout_x=0.2,
-- dropout_i=0.5,
-- dropout_h=0.2,
-- dropout_o=0.5,
-- init_weight=0.1,
-- lr=1,
-- vocab_size=10000,
-- max_epoch=180, -- start decreasing learning rate, keeping lr > 0.004
-- max_max_epoch=180,
-- max_grad_norm=5,
-- MC_dropout=false,
-- T=10}
-- -- param 17:
-- local params = {batch_size=20,
-- seq_length=20,
-- layers=2,
-- decay=1.,
-- rnn_size=200,
-- dropout_x=0.25,
-- dropout_i=0.25,
-- dropout_h=0.25,
-- dropout_o=0.25,
-- init_weight=0.1,
-- lr=1,
-- vocab_size=10000,
-- max_epoch=180, -- start decreasing learning rate, keeping lr > 0.004
-- max_max_epoch=180,
-- max_grad_norm=5,
-- MC_dropout=false,
-- T=10,
-- weight_decay=1e-6}
-- -- param 18:
-- local params = {batch_size=20,
-- seq_length=20,
-- layers=2,
-- decay=1.,
-- rnn_size=200,
-- dropout_x=0.25,
-- dropout_i=0.25,
-- dropout_h=0.25,
-- dropout_o=0.25,
-- init_weight=0.1,
-- lr=1,
-- vocab_size=10000,
-- max_epoch=180, -- start decreasing learning rate, keeping lr > 0.004
-- max_max_epoch=180,
-- max_grad_norm=5,
-- MC_dropout=false,
-- T=10,
-- weight_decay=1e-4}
-- param 19:
local params = {batch_size=20,
seq_length=20,
layers=2,
decay=1.,
rnn_size=200,
dropout_x=0.25,
dropout_i=0.25,
dropout_h=0.25,
dropout_o=0.25,
init_weight=0.1,
lr=1,
vocab_size=10000,
max_epoch=180, -- start decreasing learning rate, keeping lr > 0.004
max_max_epoch=180,
max_grad_norm=5,
MC_dropout=false,
T=10,
weight_decay=1e-5}
-- -- param 20:
-- local params = {batch_size=20,
-- seq_length=20,
-- layers=2,
-- decay=1.,
-- rnn_size=200,
-- dropout_x=0.5,
-- dropout_i=0.5,
-- dropout_h=0.5,
-- dropout_o=0.5,
-- init_weight=0.1,
-- lr=1,
-- vocab_size=10000,
-- max_epoch=180, -- start decreasing learning rate, keeping lr > 0.004
-- max_max_epoch=180,
-- max_grad_norm=5,
-- MC_dropout=false,
-- T=10,
-- weight_decay=1e-5}
-- Author: use dropout from within the script rather than nn's
local disable_dropout = false
local function local_Dropout(input, noise)
return nn.CMulTable()({input, noise})
end
local function transfer_data(x)
return x:cuda()
end
local state_train, state_valid, state_test
local model = {}
local paramx, paramdx
local function lstm(x, prev_c, prev_h, noise_i, noise_h)
-- Reshape to (batch_size, n_gates, hid_size)
-- Then slice the n_gates dimension, i.e dimension 2
local reshaped_noise_i = nn.Reshape(4,params.rnn_size)(noise_i)
local reshaped_noise_h = nn.Reshape(4,params.rnn_size)(noise_h)
local sliced_noise_i = nn.SplitTable(2)(reshaped_noise_i)
local sliced_noise_h = nn.SplitTable(2)(reshaped_noise_h)
-- Calculate all four gates
local i2h, h2h = {}, {}
for i = 1, 4 do
-- Use select table to fetch each gate
local dropped_x = local_Dropout(x, nn.SelectTable(i)(sliced_noise_i))
local dropped_h = local_Dropout(prev_h, nn.SelectTable(i)(sliced_noise_h))
i2h[i] = nn.Linear(params.rnn_size, params.rnn_size)(dropped_x)
h2h[i] = nn.Linear(params.rnn_size, params.rnn_size)(dropped_h)
end
-- Apply nonlinearity
local in_gate = nn.Sigmoid()(nn.CAddTable()({i2h[1], h2h[1]}))
local in_transform = nn.Tanh()(nn.CAddTable()({i2h[2], h2h[2]}))
local forget_gate = nn.Sigmoid()(nn.CAddTable()({i2h[3], h2h[3]}))
local out_gate = nn.Sigmoid()(nn.CAddTable()({i2h[4], h2h[4]}))
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 lstm(x, prev_c, prev_h, noise_i, noise_h)
-- -- Author: tie noise mask for all gates to keep efficiency (65% quicker, but worse results)
-- local dropped_x = local_Dropout(x, noise_i)
-- local dropped_h = local_Dropout(prev_h, noise_h)
-- -- Calculate all four gates in one go
-- local i2h = nn.Linear(params.rnn_size, 4*params.rnn_size)(dropped_x)
-- local h2h = nn.Linear(params.rnn_size, 4*params.rnn_size)(dropped_h)
-- local gates = nn.CAddTable()({i2h, h2h})
-- -- Reshape to (batch_size, n_gates, hid_size)
-- -- Then slice 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 create_network()
local x = nn.Identity()()
local y = nn.Identity()()
local prev_s = nn.Identity()()
local noise_x = nn.Identity()()
local noise_i = nn.Identity()()
local noise_h = nn.Identity()()
local noise_o = nn.Identity()()
local i = {[0] = LookupTable(params.vocab_size,
params.rnn_size)(x)}
i[0] = local_Dropout(i[0], noise_x)
local next_s = {}
local split = {prev_s:split(2 * params.layers)}
local noise_i_split = {noise_i:split(params.layers)}
local noise_h_split = {noise_h:split(params.layers)}
for layer_idx = 1, params.layers do
local prev_c = split[2 * layer_idx - 1]
local prev_h = split[2 * layer_idx]
local n_i = noise_i_split[layer_idx]
local n_h = noise_h_split[layer_idx]
local next_c, next_h = lstm(i[layer_idx - 1], prev_c, prev_h, n_i, n_h)
table.insert(next_s, next_c)
table.insert(next_s, next_h)
i[layer_idx] = next_h
end
local h2y = nn.Linear(params.rnn_size, params.vocab_size)
local dropped = local_Dropout(i[params.layers], noise_o)
local pred = nn.LogSoftMax()(h2y(dropped))
local err = nn.ClassNLLCriterion()({pred, y})
local module = nn.gModule({x, y, prev_s, noise_x, noise_i, noise_h, noise_o},
{err, nn.Identity()(next_s)})
module:getParameters():uniform(-params.init_weight, params.init_weight)
return transfer_data(module)
end
local function setup()
print("Creating a RNN LSTM network.")
local core_network = create_network()
paramx, paramdx = core_network:getParameters()
model.s = {}
model.ds = {}
model.start_s = {}
for j = 0, params.seq_length do
model.s[j] = {}
for d = 1, 2 * params.layers do
model.s[j][d] = transfer_data(torch.zeros(params.batch_size, params.rnn_size))
end
end
for d = 1, 2 * params.layers do
model.start_s[d] = transfer_data(torch.zeros(params.batch_size, params.rnn_size))
model.ds[d] = transfer_data(torch.zeros(params.batch_size, params.rnn_size))
end
-- Author: Note that the data comes in batches. We need noise to have batch by layers
-- by rnn_size dimensionality.
model.noise_i = {}
model.noise_x = {}
model.noise_xe = {} -- Author: we expand the dims of noise_x to match data dim
for j = 1, params.seq_length do
model.noise_x[j] = transfer_data(torch.zeros(params.batch_size, 1))
model.noise_xe[j] = torch.expand(model.noise_x[j], params.batch_size, params.rnn_size)
model.noise_xe[j] = transfer_data(model.noise_xe[j])
end
model.noise_h = {}
for d = 1, params.layers do
model.noise_i[d] = transfer_data(torch.zeros(params.batch_size, 4 * params.rnn_size))
model.noise_h[d] = transfer_data(torch.zeros(params.batch_size, 4 * params.rnn_size))
-- Author: tie noise mask for all gates (for efficiency - 65% quicker, but worse results)
-- model.noise_i[d] = transfer_data(torch.zeros(params.batch_size, params.rnn_size))
-- model.noise_h[d] = transfer_data(torch.zeros(params.batch_size, params.rnn_size))
end
model.noise_o = transfer_data(torch.zeros(params.batch_size, params.rnn_size))
model.core_network = core_network
model.rnns = g_cloneManyTimes(core_network, params.seq_length)
model.norm_dw = 0
model.err = transfer_data(torch.zeros(params.seq_length))
-- Author: For MC dropout we want to get pred as model output rather than the negative log probs (?)
model.pred = {}
for j = 1, params.seq_length do
model.pred[j] = transfer_data(torch.zeros(params.batch_size, params.vocab_size))
end
local y = nn.Identity()()
local pred = nn.Identity()()
local err = nn.ClassNLLCriterion()({pred, y})
model.test = transfer_data(nn.gModule({y, pred}, {err}))
end
local function reset_state(state)
state.pos = 1
if model ~= nil and model.start_s ~= nil then
for d = 1, 2 * params.layers do
model.start_s[d]:zero()
end
end
end
local function reset_ds()
for d = 1, #model.ds do
model.ds[d]:zero()
end
end
-- Author: convenience functions to handle noise
local function sample_noise(state)
-- Author: assuming state.pos is at start of input sequence
for i = 1, params.seq_length do
-- Author: cheating here - sampling iid Berns for each x; should tie over words
model.noise_x[i]:bernoulli(1 - params.dropout_x)
model.noise_x[i]:div(1 - params.dropout_x)
end
-- Author: tying over words - overriding Berns for words that were already sampled.
-- this is efficient for short sequences, but longer ones it might be better to sample
-- once for all words.
for b = 1, params.batch_size do
for i = 1, params.seq_length do
local x = state.data[state.pos + i - 1]
for j = i+1, params.seq_length do
if state.data[state.pos + j - 1] == x then
model.noise_x[j][b] = model.noise_x[i][b]
-- we only need to override the first time; afterwards subsequent are copied:
break
end
end
end
end
for d = 1, params.layers do
model.noise_i[d]:bernoulli(1 - params.dropout_i)
model.noise_i[d]:div(1 - params.dropout_i)
model.noise_h[d]:bernoulli(1 - params.dropout_h)
model.noise_h[d]:div(1 - params.dropout_h)
end
model.noise_o:bernoulli(1 - params.dropout_o)
model.noise_o:div(1 - params.dropout_o)
end
local function reset_noise()
for j = 1, params.seq_length do
model.noise_x[j]:zero():add(1)
end
for d = 1, params.layers do
model.noise_i[d]:zero():add(1)
model.noise_h[d]:zero():add(1)
end
model.noise_o:zero():add(1)
end
local function fp(state)
g_replace_table(model.s[0], model.start_s)
if state.pos + params.seq_length > state.data:size(1) then
reset_state(state)
end
-- Author: should reset noise out of function
if disable_dropout then reset_noise() else sample_noise(state) end
for i = 1, params.seq_length do
local x = state.data[state.pos]
local y = state.data[state.pos + 1]
local s = model.s[i - 1]
model.err[i], model.s[i] = unpack(model.rnns[i]:forward(
{x, y, s, model.noise_xe[i], model.noise_i, model.noise_h, model.noise_o}))
state.pos = state.pos + 1
end
-- Author: we do not keep the last state to init the next sequence, but keep it at zero
-- g_replace_table(model.start_s, model.s[params.seq_length])
return model.err
end
local function fp_MC(state)
g_replace_table(model.s[0], model.start_s)
if state.pos + params.seq_length > state.data:size(1) then
reset_state(state)
end
-- Author: reset pred
for i = 1, params.seq_length do
model.pred[i]:zero()
end
local T = single_stochastic_dropout_pass and 1 or params.T
for t = 1, T do
sample_noise(state)
for i = 1, params.seq_length do
local x = state.data[state.pos + i - 1]
local y = state.data[state.pos + i]
local s = model.s[i - 1]
-- Author: we sample several times and average
model.s[i] = model.rnns[i]:forward({x, y, s,
model.noise_xe[i], model.noise_i, model.noise_h, model.noise_o})[2]
local pred = model.rnns[i].outnode.data.mapindex[1].input[1]
model.pred[i]:add(pred:exp()) -- does this underflow?
end
end
for i = 1, params.seq_length do
local y = state.data[state.pos + i]
model.pred[i]:log():add(-torch.log(T))
model.err[i] = model.test:forward({y, model.pred[i]})
end
state.pos = state.pos + params.seq_length
-- Author: we do not keep the last state to init the next sequence, but keep it at zero
-- g_replace_table(model.start_s, model.s[params.seq_length])
return model.err
end
local function bp(state)
-- Author: we truncate the derivative at seq_length, which is equivalent
-- to using sequences of length seq_length but with smarter initialisation
-- than putting zeros for the first state. This is easier than bucketing,
-- but carries internal states over <eos> which is bad. Especially because
-- that means we use shorter sequences for each sentence. Note that it seems
-- bad to reset ds if we use the prev s?
paramdx:zero()
reset_ds()
for i = params.seq_length, 1, -1 do
state.pos = state.pos - 1
local x = state.data[state.pos]
local y = state.data[state.pos + 1]
local s = model.s[i - 1]
local derr = transfer_data(torch.ones(1))
local tmp = model.rnns[i]:backward( -- Author: do we need model.noise_x[i+1]?
{x, y, s, model.noise_xe[i], model.noise_i, model.noise_h, model.noise_o},
{derr, model.ds})[3]
g_replace_table(model.ds, tmp)
cutorch.synchronize()
end
state.pos = state.pos + params.seq_length
model.norm_dw = paramdx:norm()
if model.norm_dw > params.max_grad_norm then
local shrink_factor = params.max_grad_norm / model.norm_dw
paramdx:mul(shrink_factor)
end
paramx:add(paramdx:mul(-params.lr))
-- Author: add weight decay
paramx:add(-params.weight_decay, paramx)
end
local function run_valid()
reset_state(state_valid)
-- Author: disable dropout for standard dropout
if not params.MC_dropout then
disable_dropout = true
end
local len = (state_valid.data:size(1) - 1) / (params.seq_length)
local perp = 0
for i = 1, len do
local p = params.MC_dropout and fp_MC(state_valid) or fp(state_valid)
perp = perp + p:mean()
end
print("Validation set perplexity : " .. g_f3(torch.exp(perp / len)))
if not params.MC_dropout then
disable_dropout = false
end
end
local function run_test()
-- follows the same code of validation, using average perp of non-overlapping sequences
reset_state(state_test)
-- Author: disable dropout for standard dropout
if not params.MC_dropout then
disable_dropout = true
end
local len = (state_test.data:size(1) - 1) / (params.seq_length)
local perp = 0
for i = 1, len do
local p = params.MC_dropout and fp_MC(state_test) or fp(state_test)
perp = perp + p:mean()
end
print("Test set perplexity : " .. g_f3(torch.exp(perp / len)))
if not params.MC_dropout then
disable_dropout = false
end
end
local function run_test_all()
-- follows the same code of validation, but with overlapping sequences using last seq perp
-- Author: need to test this function!
reset_state(state_test)
-- Author: disable test time dropout for standard dropout approx
if not params.MC_dropout then
disable_dropout = true
end
local len = state_test.data:size(1) - params.seq_length
local perp = 0
for i = 1, len do
state_test.pos = i
local p = params.MC_dropout and fp_MC(state_test) or fp(state_test)
perp = perp + p[params.seq_length] -- use perp of last seq element p(s_l | s_1 .. s_lm1)
end
print("Test set perplexity : " .. g_f3(torch.exp(perp / len)))
if not params.MC_dropout then
disable_dropout = false
end
end
local function run_test_orig()
reset_state(state_test)
if params.MC_dropout then
sample_noise()
else
reset_noise()
end
local perp = 0
local len = state_test.data:size(1)
g_replace_table(model.s[0], model.start_s)
for i = 1, (len - 1) do
local x = state_test.data[i]
local y = state_test.data[i + 1]
perp_tmp, model.s[1] = unpack(model.rnns[1]:forward(
{x, y, model.s[0], model.noise_i, model.noise_h, model.noise_o}))
perp = perp + perp_tmp[1]
g_replace_table(model.s[0], model.s[1])
end
print("Test set perplexity : " .. g_f3(torch.exp(perp / (len - 1))))
end
local function main()
g_init_gpu(arg)
state_train = {data=transfer_data(ptb.traindataset(params.batch_size))}
state_valid = {data=transfer_data(ptb.validdataset(params.batch_size))}
state_test = {data=transfer_data(ptb.testdataset(params.batch_size))}
print("Network parameters:")
print(params)
local states = {state_train, state_valid, state_test}
for _, state in pairs(states) do
reset_state(state)
end
setup()
local step = 0
local epoch = 0
local total_cases = 0
local beginning_time = torch.tic()
local start_time = torch.tic()
print("Starting training.")
local epoch_size = torch.floor(state_train.data:size(1) / params.seq_length)
local perps
while epoch < params.max_max_epoch do
local perp = fp(state_train):mean()
if perps == nil then
perps = torch.zeros(epoch_size):add(perp)
end
perps[step % epoch_size + 1] = perp
step = step + 1
bp(state_train)
total_cases = total_cases + params.seq_length * params.batch_size
epoch = step / epoch_size
if step % torch.round(epoch_size / 10) == 10 then
local wps = torch.floor(total_cases / torch.toc(start_time))
local since_beginning = g_d(torch.toc(beginning_time) / 60)
print('epoch = ' .. g_f3(epoch) ..
', train perp. = ' .. g_f3(torch.exp(perps:mean())) ..
', wps = ' .. wps ..
', dw:norm() = ' .. g_f3(model.norm_dw) ..
', lr = ' .. g_f3(params.lr) ..
', since beginning = ' .. since_beginning .. ' mins.')
end
if step % epoch_size == 0 then
params.MC_dropout = false
run_valid()
-- params.MC_dropout = true
-- run_valid()
if epoch > params.max_epoch then
params.lr = params.lr / params.decay
end
end
if step % 33 == 0 then
cutorch.synchronize()
collectgarbage()
end
end
params.MC_dropout = false
run_test()
params.MC_dropout = true
run_test()
params.MC_dropout = false
run_test_all()
-- params.MC_dropout = true
-- run_test_all()
print("Training is over.")
end
main()