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

test_debug.lua « test - github.com/torch/nngraph.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f5c8b06f45d8c6cd5a601703465eab11fee6ece3 (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
local totem = require 'totem'
require 'nngraph'
local tests = totem.TestSuite()
local tester = totem.Tester()

function tests.whatIsThisInTheInput()
  tester:assertErrorPattern(
      function()
        local inp1, inp2 = nn.Identity()(), nn.Identity() -- missing 2nd parens
        local lin = nn.Linear(20, 10)(nn.CMulTable(){inp1, inp2})
      end,
      'inputs%[2%] is an nn%.Module, specifically a nn%.Identity, but the ' ..
      'only valid thing to pass is an instance of nngraph%.Node')

  tester:assertErrorPattern(
      function()
        -- pass-through module, again with same mistake
        local graphNode, nnModule = nn.Identity()(), nn.Identity()
        return nn.gModule({graphNode, nnModule}, {graphNode})
      end,
      'inputs%[2%] is an nn%.Module, specifically a nn%.Identity, but the ' ..
      'only valid thing to pass is an instance of nngraph%.Node')

  tester:assertErrorPattern(
      function()
        local input = nn.Identity()()
        local out1 = nn.Linear(20, 10)(input)
        local out2 = nn.Sigmoid()(input)
        local unconnectedOut = nn.Linear(2, 3)
        return nn.gModule({input}, {out1, out2, unconnectedOut})
      end,
      'outputs%[3%] is an nn%.Module, specifically a nn%.Linear, but the ' ..
      'only valid thing to pass is an instance of nngraph%.Node')

  -- Check for detecting a nil in the middle of a table.
  tester:assertErrorPattern(
      function()
        local input = nn.Identity()()
        local out1 = nn.Tanh()(input)
        local out2 = nn.Sigmoid()(input)
        -- nil here is simulating a mis-spelt variable name
        return nn.gModule({input}, {out1, nil, out2})
      end,
      'outputs%[2%] is nil %(typo / bad index%?%)')

  tester:assertErrorPattern(
      function()
        -- Typo variable name returns nil, meaning an empty table
        local input = nn.Identity()({aNonExistentVariable})
      end,
      'cannot pass an empty table of inputs%. To indicate no incoming ' ..
      'connections, leave the second set of parens blank%.')
end

function tests.splitUnused()
  -- Need to do debuginfo on the same lines as the other code here to match
  -- what debug.getinfo inside those calls will return
  local dInfoDeclare, dInfoSplit
  local input = nn.Identity()(); dInfoDeclare = debug.getinfo(1, 'Sl')
  local output, unused = input:split(2); dInfoSplit = debug.getinfo(1, 'Sl')

  local function willCrash()
    return nn.gModule({input}, {output})
  end

  -- Work out what strings will be in the error message
  local declareLoc = string.format('%%[%s%%]:%d_',
                                   dInfoDeclare.short_src,
                                   dInfoDeclare.currentline)
  local splitLoc = string.format('%%[%s%%]:%d',
                                 dInfoSplit.short_src,
                                 dInfoSplit.currentline)

  tester:assertErrorPattern(
      willCrash,
      '1 of split%(2%) outputs from the node declared at ' ..
      declareLoc .. ' split at ' .. splitLoc .. '%-mnode are unused')
end

tester:add(tests):run()