forked from karpathy/neuraltalk2
-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_language_model.lua
321 lines (274 loc) · 10.3 KB
/
test_language_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
--[[
Unit tests for the LanguageModel implementation, making sure
that nothing crashes, that we can overfit a small dataset
and that everything gradient checks.
--]]
require 'torch'
require 'misc.LanguageModel'
local gradcheck = require 'misc.gradcheck'
local tests = {}
local tester = torch.Tester()
-- validates the size and dimensions of a given
-- tensor a to be size given in table sz
function tester:assertTensorSizeEq(a, sz)
tester:asserteq(a:nDimension(), #sz)
for i=1,#sz do
tester:asserteq(a:size(i), sz[i])
end
end
-- Test the API of the Language Model
local function forwardApiTestFactory(dtype)
if dtype == 'torch.CudaTensor' then
require 'cutorch'
require 'cunn'
end
local function f()
-- create LanguageModel instance
local opt = {}
opt.vocab_size = 5
opt.input_encoding_size = 11
opt.rnn_size = 8
opt.num_layers = 2
opt.dropout = 0
opt.seq_length = 7
opt.batch_size = 10
local lm = nn.LanguageModel(opt)
local crit = nn.LanguageModelCriterion()
lm:type(dtype)
crit:type(dtype)
-- construct some input to feed in
local seq = torch.LongTensor(opt.seq_length, opt.batch_size):random(opt.vocab_size)
-- make sure seq can be padded with zeroes and that things work ok
seq[{ {4, 7}, 1 }] = 0
seq[{ {5, 7}, 6 }] = 0
local imgs = torch.randn(opt.batch_size, opt.input_encoding_size):type(dtype)
local output = lm:forward{imgs, seq}
tester:assertlt(torch.max(output:view(-1)), 0) -- log probs should be <0
-- the output should be of size (seq_length + 2, batch_size, vocab_size + 1)
-- where the +1 is for the special END token appended at the end.
tester:assertTensorSizeEq(output, {opt.seq_length+2, opt.batch_size, opt.vocab_size+1})
local loss = crit:forward(output, seq)
local gradOutput = crit:backward(output, seq)
tester:assertTensorSizeEq(gradOutput, {opt.seq_length+2, opt.batch_size, opt.vocab_size+1})
-- make sure the pattern of zero gradients is as expected
local gradAbs = torch.max(torch.abs(gradOutput), 3):view(opt.seq_length+2, opt.batch_size)
local gradZeroMask = torch.eq(gradAbs,0)
local expectedGradZeroMask = torch.ByteTensor(opt.seq_length+2,opt.batch_size):zero()
expectedGradZeroMask[{ {1}, {} }]:fill(1) -- first time step should be zero grad (img was passed in)
expectedGradZeroMask[{ {6,9}, 1 }]:fill(1)
expectedGradZeroMask[{ {7,9}, 6 }]:fill(1)
tester:assertTensorEq(gradZeroMask:float(), expectedGradZeroMask:float(), 1e-8)
local gradInput = lm:backward({imgs, seq}, gradOutput)
tester:assertTensorSizeEq(gradInput[1], {opt.batch_size, opt.input_encoding_size})
tester:asserteq(gradInput[2]:nElement(), 0, 'grad on seq should be empty tensor')
end
return f
end
-- test just the language model alone (without the criterion)
local function gradCheckLM()
local dtype = 'torch.DoubleTensor'
local opt = {}
opt.vocab_size = 5
opt.input_encoding_size = 4
opt.rnn_size = 8
opt.num_layers = 2
opt.dropout = 0
opt.seq_length = 7
opt.batch_size = 6
local lm = nn.LanguageModel(opt)
local crit = nn.LanguageModelCriterion()
lm:type(dtype)
crit:type(dtype)
local seq = torch.LongTensor(opt.seq_length, opt.batch_size):random(opt.vocab_size)
seq[{ {4, 7}, 1 }] = 0
seq[{ {5, 7}, 4 }] = 0
local imgs = torch.randn(opt.batch_size, opt.input_encoding_size):type(dtype)
-- evaluate the analytic gradient
local output = lm:forward{imgs, seq}
local w = torch.randn(output:size(1), output:size(2), output:size(3))
-- generate random weighted sum criterion
local loss = torch.sum(torch.cmul(output, w))
local gradOutput = w
local gradInput, dummy = unpack(lm:backward({imgs, seq}, gradOutput))
-- create a loss function wrapper
local function f(x)
local output = lm:forward{x, seq}
local loss = torch.sum(torch.cmul(output, w))
return loss
end
local gradInput_num = gradcheck.numeric_gradient(f, imgs, 1, 1e-6)
-- print(gradInput)
-- print(gradInput_num)
-- local g = gradInput:view(-1)
-- local gn = gradInput_num:view(-1)
-- for i=1,g:nElement() do
-- local r = gradcheck.relative_error(g[i],gn[i])
-- print(i, g[i], gn[i], r)
-- end
tester:assertTensorEq(gradInput, gradInput_num, 1e-4)
tester:assertlt(gradcheck.relative_error(gradInput, gradInput_num, 1e-8), 1e-4)
end
local function gradCheck()
local dtype = 'torch.DoubleTensor'
local opt = {}
opt.vocab_size = 5
opt.input_encoding_size = 4
opt.rnn_size = 8
opt.num_layers = 2
opt.dropout = 0
opt.seq_length = 7
opt.batch_size = 6
local lm = nn.LanguageModel(opt)
local crit = nn.LanguageModelCriterion()
lm:type(dtype)
crit:type(dtype)
local seq = torch.LongTensor(opt.seq_length, opt.batch_size):random(opt.vocab_size)
seq[{ {4, 7}, 1 }] = 0
seq[{ {5, 7}, 4 }] = 0
local imgs = torch.randn(opt.batch_size, opt.input_encoding_size):type(dtype)
-- evaluate the analytic gradient
local output = lm:forward{imgs, seq}
local loss = crit:forward(output, seq)
local gradOutput = crit:backward(output, seq)
local gradInput, dummy = unpack(lm:backward({imgs, seq}, gradOutput))
-- create a loss function wrapper
local function f(x)
local output = lm:forward{x, seq}
local loss = crit:forward(output, seq)
return loss
end
local gradInput_num = gradcheck.numeric_gradient(f, imgs, 1, 1e-6)
-- print(gradInput)
-- print(gradInput_num)
-- local g = gradInput:view(-1)
-- local gn = gradInput_num:view(-1)
-- for i=1,g:nElement() do
-- local r = gradcheck.relative_error(g[i],gn[i])
-- print(i, g[i], gn[i], r)
-- end
tester:assertTensorEq(gradInput, gradInput_num, 1e-4)
tester:assertlt(gradcheck.relative_error(gradInput, gradInput_num, 1e-8), 5e-4)
end
local function overfit()
local dtype = 'torch.DoubleTensor'
local opt = {}
opt.vocab_size = 5
opt.input_encoding_size = 7
opt.rnn_size = 24
opt.num_layers = 1
opt.dropout = 0
opt.seq_length = 7
opt.batch_size = 6
local lm = nn.LanguageModel(opt)
local crit = nn.LanguageModelCriterion()
lm:type(dtype)
crit:type(dtype)
local seq = torch.LongTensor(opt.seq_length, opt.batch_size):random(opt.vocab_size)
seq[{ {4, 7}, 1 }] = 0
seq[{ {5, 7}, 4 }] = 0
local imgs = torch.randn(opt.batch_size, opt.input_encoding_size):type(dtype)
local params, grad_params = lm:getParameters()
print('number of parameters:', params:nElement(), grad_params:nElement())
local lstm_params = 4*(opt.input_encoding_size + opt.rnn_size)*opt.rnn_size + opt.rnn_size*4*2
local output_params = opt.rnn_size * (opt.vocab_size + 1) + opt.vocab_size+1
local table_params = (opt.vocab_size + 1) * opt.input_encoding_size
local expected_params = lstm_params + output_params + table_params
print('expected:', expected_params)
local function lossFun()
grad_params:zero()
local output = lm:forward{imgs, seq}
local loss = crit:forward(output, seq)
local gradOutput = crit:backward(output, seq)
lm:backward({imgs, seq}, gradOutput)
return loss
end
local loss
local grad_cache = grad_params:clone():fill(1e-8)
print('trying to overfit the language model on toy data:')
for t=1,30 do
loss = lossFun()
-- test that initial loss makes sense
if t == 1 then tester:assertlt(math.abs(math.log(opt.vocab_size+1) - loss), 0.1) end
grad_cache:addcmul(1, grad_params, grad_params)
params:addcdiv(-1e-1, grad_params, torch.sqrt(grad_cache)) -- adagrad update
print(string.format('iteration %d/30: loss %f', t, loss))
end
-- holy crap adagrad destroys the loss function!
tester:assertlt(loss, 0.2)
end
-- check that we can call :sample() and that correct-looking things happen
local function sample()
local dtype = 'torch.DoubleTensor'
local opt = {}
opt.vocab_size = 5
opt.input_encoding_size = 4
opt.rnn_size = 8
opt.num_layers = 2
opt.dropout = 0
opt.seq_length = 7
opt.batch_size = 6
local lm = nn.LanguageModel(opt)
local imgs = torch.randn(opt.batch_size, opt.input_encoding_size):type(dtype)
local seq = lm:sample(imgs)
tester:assertTensorSizeEq(seq, {opt.seq_length, opt.batch_size})
tester:asserteq(seq:type(), 'torch.LongTensor')
tester:assertge(torch.min(seq), 1)
tester:assertle(torch.max(seq), opt.vocab_size+1)
print('\nsampled sequence:')
print(seq)
end
-- check that we can call :sample_beam() and that correct-looking things happen
-- these are not very exhaustive tests and basic sanity checks
local function sample_beam()
local dtype = 'torch.DoubleTensor'
torch.manualSeed(1)
local opt = {}
opt.vocab_size = 10
opt.input_encoding_size = 4
opt.rnn_size = 8
opt.num_layers = 1
opt.dropout = 0
opt.seq_length = 7
opt.batch_size = 6
local lm = nn.LanguageModel(opt)
local imgs = torch.randn(opt.batch_size, opt.input_encoding_size):type(dtype)
local seq_vanilla, logprobs_vanilla = lm:sample(imgs)
local seq, logprobs = lm:sample(imgs, {beam_size = 1})
-- check some basic I/O, types, etc.
tester:assertTensorSizeEq(seq, {opt.seq_length, opt.batch_size})
tester:asserteq(seq:type(), 'torch.LongTensor')
tester:assertge(torch.min(seq), 0)
tester:assertle(torch.max(seq), opt.vocab_size+1)
-- doing beam search with beam size 1 should return exactly what we had before
print('')
print('vanilla sampling:')
print(seq_vanilla)
print('beam search sampling with beam size 1:')
print(seq)
tester:assertTensorEq(seq_vanilla, seq, 0) -- these are LongTensors, expect exact match
tester:assertTensorEq(logprobs_vanilla, logprobs, 1e-6) -- logprobs too
-- doing beam search with higher beam size should yield higher likelihood sequences
local seq2, logprobs2 = lm:sample(imgs, {beam_size = 8})
local logsum = torch.sum(logprobs, 1)
local logsum2 = torch.sum(logprobs2, 1)
print('')
print('beam search sampling with beam size 1:')
print(seq)
print('beam search sampling with beam size 8:')
print(seq2)
print('logprobs:')
print(logsum)
print(logsum2)
-- the logprobs should always be >=, since beam_search is better argmax inference
tester:assert(torch.all(torch.gt(logsum2, logsum)))
end
tests.doubleApiForwardTest = forwardApiTestFactory('torch.DoubleTensor')
tests.floatApiForwardTest = forwardApiTestFactory('torch.FloatTensor')
tests.cudaApiForwardTest = forwardApiTestFactory('torch.CudaTensor')
tests.gradCheck = gradCheck
tests.gradCheckLM = gradCheckLM
tests.overfit = overfit
tests.sample = sample
tests.sample_beam = sample_beam
tester:add(tests)
tester:run()