Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test_nngraph.lua « test - github.com/torch/nngraph.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e3d89823cb8eec0445ff6f0f7f3b3bd838579abb (plain)
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

require 'totem'
require 'nngraph'
local test = {}
local tester = totem.Tester()

local function checkGradients(...)
   totem.nn.checkGradients(tester, ...)
end

function test.test_oneOutput()
   local in1 = nn.Identity()()
   local out1 = nn.Identity()(in1)
   local module = nn.gModule({in1}, {out1})

   local input = torch.Tensor({1})
   module:forward(input)
   tester:eq(module.output, torch.Tensor{1}, "output")
   local gradInput = module:backward(input, torch.Tensor({-123}))
   tester:eq(gradInput, torch.Tensor{-123}, "gradInput")

   local input2 = torch.Tensor({2})
   module:forward(input2)
   tester:eq(module.output, torch.Tensor{2}, "output for input2")
   gradInput = module:backward(input2, torch.Tensor({-2}))
   tester:eq(gradInput, torch.Tensor{-2}, "expecting a recomputed gradInput")
end


function test.test_twoOutputs()
   local in1 = nn.Identity()()
   local out1 = nn.Identity()(in1)
   local out2 = nn.Identity()(in1)
   local module = nn.gModule({in1}, {out1, out2})

   local input = torch.Tensor({1})
   module:forward(input)
   local gradInput = module:backward(input, {torch.Tensor({-2}), torch.Tensor({-3})})
   tester:eq(gradInput, torch.Tensor{-5}, "gradInput of a fork")
   checkGradients(module, input)
end

function test.test_twoGradOutputs()
   local in1 = nn.Sigmoid()()
   local splitTable = nn.SplitTable(1)({in1})
   local out1, out2 = splitTable:split(2)
   local module = nn.gModule({in1}, {out1, out2})

   local input = torch.randn(2, 3)
   local output = module:forward(input)
   assert(#output == 2, "wrong number of outputs")
   module:backward(input, {torch.randn(3), torch.randn(3)})
   checkGradients(module, input)
end

function test.test_twoInputs()
   local in1 = nn.Identity()()
   local in2 = nn.Identity()()
   local prevH, prevCell = in2:split(2)

   local out1 = nn.CMulTable()({in1, prevH, prevCell})
   local module = nn.gModule({in1, in2}, {out1})

   local input = {torch.randn(3), {torch.randn(3), torch.randn(3)}}
   module:forward(input)
   local gradInput = module:backward(input, torch.randn(3))
   assert(#gradInput == 2, "wrong number of gradInputs")
   assert(type(gradInput[2]) == "table", "wrong gradInput[2] type")
   checkGradients(module, input)
end

function test.test_twoInputs2()
   local in1 = nn.Sigmoid()()
   local in2 = nn.Sigmoid()()
   local module = nn.gModule({in1, in2}, {in1, in2, nn.Sigmoid()(in1)})

   local input = {torch.randn(3), torch.randn(3)}
   module:forward(input)
   local gradInput = module:backward(input, {torch.randn(3), torch.randn(3), torch.randn(3)})
   checkGradients(module, input)
end

function test.test_splitDebugLabels()
   local node = nn.Identity()()
   node.data.annotations._debugLabel = "node"
   local node1, node2 = node:split(2)
   assert(node1.data.annotations._debugLabel == "node-1")
   assert(node2.data.annotations._debugLabel == "node-2")
end

function test.test_identity()
   local in1 = nn.Identity()()
   local in2 = nn.Identity()()
   local module = nn.gModule({in1, in2}, {in1, in2, nn.Identity()(in1)})

   local input = {torch.randn(3), torch.randn(3)}
   module:forward(input)
   module:backward(input, {torch.randn(3), torch.randn(3), torch.randn(3)})
   checkGradients(module, input)
end

function test.test_gradInputType()
   local xInput = torch.randn(3)
   local h = torch.randn(3)

   local x = nn.Identity()()
   local prevRnnState = nn.Identity()()
   local prevH1, prevCell = prevRnnState:split(2)
   local prevH = prevH1

   local cellOut = nn.CAddTable()({
      nn.CMulTable()({x, prevH}),
      nn.CMulTable()({prevH, prevCell})})
      local module = nn.gModule({x, prevRnnState}, {cellOut})

      local c = torch.randn(h:size())
      local prevRnnState = {h, c}
      local input = {xInput, prevRnnState}
      local output = module:forward(input)

      local gradOutput = torch.randn(h:size())
      local gradInput = module:backward(input, gradOutput)

      local unpack = unpack or table.unpack
      local gradX, gradPrevState = unpack(gradInput)
      local gradPrevH, gradPrevCell = unpack(gradPrevState)
      assert(type(gradPrevH) == type(h), "wrong gradPrevH type")

      tester:eq(type(gradPrevH), type(h), "wrong gradPrevH type")
      tester:eq(gradPrevH:size(), h:size(), "wrong gradPrevH size")
      checkGradients(module, input)
   end

   function test.test_tabularInput()
      local in1 = nn.SplitTable(1)()
      local out1 = nn.CAddTable()(in1)
      local module = nn.gModule({in1}, {out1})

      local input = torch.randn(2, 3)
      checkGradients(module, input)
   end

   function test.test_extraTable()
      local in1 = nn.Identity()()
      local out1 = nn.Identity()(in1)
      local module = nn.gModule({in1}, {out1})

      local input = torch.Tensor({123})
      tester:eq(module:forward(input), input, "simple output")
      tester:eq(module:forward({input}), {input}, "tabular output")
   end

   function test.test_accGradParameters()
      local input = torch.randn(10)

      local in1 = nn.CMul(input:nElement())()
      local out1 = nn.Identity()(in1)
      local out2 = nn.Identity()(in1)
      local module = nn.gModule({in1}, {out1, out2})
      checkGradients(module, input)
   end

   function test.test_example1()
      local x1 = nn.Linear(20,10)()
      local mout = nn.Linear(10,1)(nn.Tanh()(nn.Linear(10,10)(nn.Tanh()(x1))))
      local mlp = nn.gModule({x1},{mout})

      local x = torch.rand(20)
      checkGradients(mlp, x)
   end

   function test.test_example2()
      local x1=nn.Linear(20,20)()
      local x2=nn.Linear(10,10)()
      local m0=nn.Linear(20,1)(nn.Tanh()(x1))
      local m1=nn.Linear(10,1)(nn.Tanh()(x2))
      local madd=nn.CAddTable()({m0,m1})
      local m2=nn.Sigmoid()(madd)
      local m3=nn.Tanh()(madd)
      local gmod = nn.gModule({x1,x2},{m2,m3})

      local x = torch.rand(20)
      local y = torch.rand(10)
      checkGradients(gmod, {x, y})
   end

   function test.test_example3()
      local m = nn.Sequential()
      m:add(nn.SplitTable(1))
      m:add(nn.ParallelTable():add(nn.Linear(10,20)):add(nn.Linear(10,30)))
      local input = nn.Identity()()
      local input1,input2 = m(input):split(2)
      local m3 = nn.JoinTable(1)({input1,input2})
      local g = nn.gModule({input},{m3})

      local indata = torch.rand(2,10)
      checkGradients(g, indata)
   end

   function test.test_example4()
      local input = nn.Identity()()
      local L1 = nn.Tanh()(nn.Linear(1,2)(input))
      local L2 = nn.Tanh()(nn.Linear(3,6)(nn.JoinTable(1)({input,L1})))
      local L3 = nn.Tanh()(nn.Linear(8,16)(nn.JoinTable(1)({L1,L2})))
      local g = nn.gModule({input},{L3})

      local indata = torch.rand(1)
      checkGradients(g, indata)
   end

   function test.test_type()
      local in1 = nn.Linear(20,10)()
      local out1 = nn.Linear(10,1)(nn.Tanh()(nn.Linear(10,10)(nn.Tanh()(in1))))
      local module = nn.gModule({in1}, {out1})
      local input = torch.rand(20)
      local output = module:forward(input)
      module:backward(input, output)
      tester:eq(torch.typename(output), "torch.DoubleTensor")
      tester:eq(torch.typename(module.output), "torch.DoubleTensor")
      tester:eq(torch.typename(module.gradInput), "torch.DoubleTensor")
      tester:eq(torch.typename(module.innode.data.input[1]), "torch.DoubleTensor")
      tester:eq(torch.typename(module.outnode.data.input[1]), "torch.DoubleTensor")
      tester:eq(torch.typename(module.forwardnodes[1].data.input[1]), "torch.DoubleTensor")

      module:float()
      local output = module:forward(input:float())
      tester:eq(torch.typename(output), "torch.FloatTensor")
      tester:eq(torch.typename(module.output), "torch.FloatTensor")
      tester:eq(torch.typename(module.gradInput), "torch.FloatTensor")
      tester:eq(torch.typename(module.innode.data.input[1]), "torch.FloatTensor")
      tester:eq(torch.typename(module.outnode.data.input[1]), "torch.FloatTensor")
      tester:eq(torch.typename(module.forwardnodes[1].data.input[1]), "torch.FloatTensor")
   end

   function test.test_nestedGradInput()
      local x = nn.Identity()()
      local h1 = nn.Sequential():add(nn.JoinTable(2)):add(nn.Tanh())
      local h2 = nn.Sequential():add(nn.JoinTable(2)):add(nn.Identity())
      local out = nn.CAddTable()({h1(x), h2(x)})

      local model = nn.gModule({x}, {out})

      local input = {}
      input[1] = torch.randn(3, 3)
      input[2] = torch.randn(3, 3)
      input[3] = torch.randn(3, 3)

      checkGradients(model, input)

      local input = {}
      input[1] = torch.randn(2, 3)
      input[2] = torch.randn(2, 3)
      input[3] = torch.randn(2, 3)

      checkGradients(model, input)
   end

   function test.test_unusedInput()
      local x = nn.Identity()()
      local h = nn.Identity()()
      local h2 = nn.Identity()()

      local ok, result = pcall(nn.gModule, {x, h}, {x})
      assert(not ok, "the unused input should be detected")
   end

   function test.test_unusedChild()
      local prevState = nn.Identity()()
      local h, cell = prevState:split(2)

      local ok, result = pcall(nn.gModule, {prevState}, {h})
      assert(not ok, "the unused cell should be detected")
   end

   function test.test_nilInput()
      local ok, result = pcall(function() nn.Sigmoid()(nil) end)
      assert(not ok, "the nil input should be detected")
   end

   function test.test_unusedNode()
      local in1 = nn.Identity()()
      local in2 = nn.Identity()()
      local middleResult = nn.Sigmoid()(in2)
      local out1 = nn.Sigmoid()(in1)

      local ok, result = pcall(nn.gModule, {in1, in2}, {out1})
      assert(not ok, "the unused middleResult should be detected")
   end

   function test.test_usageAfterSplit()
      local prevState = nn.Identity()()
      local h, cell = prevState:split(2)
      local nextState = nn.Identity()(prevState)
      local transformed = nn.Sigmoid()(cell)

      local model = nn.gModule({prevState}, {h, nextState, transformed})
      local nHidden = 10
      local input = {torch.randn(nHidden), torch.randn(nHidden)}
      checkGradients(model, input)
   end

   function test.test_resizeNestedAs()
      local in1 = nn.Identity()()
      local out1 = nn.Identity()(in1)
      local out2 = nn.Identity()(in1)

      local net = nn.gModule({in1}, {out1, out2})
      local input = {torch.randn(10), {torch.randn(3), torch.randn(4)}}
      net:forward(input)
      net:backward(input, net.output)
      checkGradients(net, input)

      input = {torch.randn(10), {torch.randn(3), torch.randn(4), torch.randn(5)}}
      net:forward(input)
      net:backward(input, net.output)
      checkGradients(net, input)

      input = {torch.randn(10), {torch.randn(3), torch.randn(4)}}
      net:forward(input)
      local gradInput = net:backward(input, net.output)
      tester:eq(#(gradInput[2]), 2, "gradInput[2] size")
      checkGradients(net, input)
   end


   function test.test_annotateGraph()
      local input = nn.Identity()():annotate(
      {name = 'Input', description = 'DescA',
      graphAttributes = {color = 'red'}})

      local hidden_a = nn.Linear(10, 10)(input):annotate(
      {name = 'Hidden A', description = 'DescB',
      graphAttributes = {color = 'blue', fontcolor='green', tooltip = 'I am green'}})
      local hidden_b = nn.Sigmoid()(hidden_a)
      local output = nn.Linear(10, 10)(hidden_b)
      local net = nn.gModule({input}, {output})

      tester:assert(hidden_a:label():match('DescB'))
      local fg_tmpfile = os.tmpname()
      local bg_tmpfile = os.tmpname()
      graph.dot(net.fg, 'Test', fg_tmpfile)
      graph.dot(net.fg, 'Test BG', bg_tmpfile)

      local function checkDotFile(tmpfile)
         local dotcontent = io.open(tmpfile .. '.dot', 'r'):read("*all")
         tester:assert(
         dotcontent:match('%[color=red.*label=%"Input.*DescA.*%".*%]'))
         tester:assert(
         dotcontent:match(
         '%[.*fontcolor=green.*label=%"Hidden A.*DescB.*%".*%]'))
         tester:assert(
         dotcontent:match('%[color=blue.*label=%".*DescB.*%".*%]'))
         tester:assert(
         dotcontent:match(
         '%[.*label=%".*DescB.*%".*tooltip=%"I am green%".*%]'))
      end

      checkDotFile(fg_tmpfile)
      checkDotFile(bg_tmpfile)
   end

   function test.test_splitMore()
      local nSplits = 2
      local in1 = nn.Identity()()
      local out1, out2 = nn.SplitTable(2)(in1):split(nSplits)

      local model = nn.gModule({in1}, {out1, out2})
      local input = torch.randn(10, nSplits + 1)
      local ok, result = pcall(model.forward, model, input)
      assert(not ok, "the extra input to split should be detected")
   end

   function test.test_splitLess()
      local nSplits = 3
      local in1 = nn.Identity()()
      local out1, out2, out3 = nn.SplitTable(2)(in1):split(nSplits)

      local model = nn.gModule({in1}, {out1, out2, out3})
      local input = torch.randn(10, nSplits - 1)
      local ok, result = pcall(model.forward, model, input)
      assert(not ok, "the missing input to split should be detected")
   end

   tester:add(test):run()