OCD cosmetics.
[dagnn.git] / test-dagnn.lua
1 #!/usr/bin/env luajit
2
3 --[[
4
5    Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/
6    Written by Francois Fleuret <francois.fleuret@idiap.ch>
7
8    This file is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License version 3 as
10    published by the Free Software Foundation.
11
12    It is distributed in the hope that it will be useful, but WITHOUT
13    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15    License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this file.  If not, see <http://www.gnu.org/licenses/>.
19
20 ]]--
21
22 require 'torch'
23 require 'nn'
24
25 -- require 'cunn'
26
27 require 'dagnn'
28
29 torch.setdefaulttensortype('torch.DoubleTensor')
30 torch.manualSeed(1)
31
32 function checkGrad(model, criterion, input, target, epsilon)
33    local params, gradParams = model:getParameters()
34
35    local epsilon = epsilon or 1e-5
36
37    local output = model:forward(input)
38    local loss = criterion:forward(output, target)
39    local gradOutput = criterion:backward(output, target)
40    gradParams:zero()
41    model:backward(input, gradOutput)
42    local analyticalGradParam = gradParams:clone()
43
44    local err = 0
45
46    for i = 1, params:size(1) do
47       local x = params[i]
48
49       params[i] = x - epsilon
50       local output0 = model:forward(input)
51       local loss0 = criterion:forward(output0, target)
52
53       params[i] = x + epsilon
54       local output1 = model:forward(input)
55       local loss1 = criterion:forward(output1, target)
56
57       params[i] = x
58
59       local ana = analyticalGradParam[i]
60       local num = (loss1 - loss0) / (2 * epsilon)
61
62       if num ~= ana then
63          err = math.max(err, math.abs(num - ana) / math.max(epsilon, math.abs(num)))
64       end
65    end
66
67    return err
68 end
69
70 function printTensorTable(t)
71    if torch.type(t) == 'table' then
72       for i, t in pairs(t) do
73          print('-- ELEMENT [' .. i .. '] --')
74          printTensorTable(t)
75       end
76    else
77       print(tostring(t))
78    end
79 end
80
81 --               +-- Linear(10, 10) --> ReLU --> d -->
82 --              /                               /
83 --             /                               /
84 --  --> a --> b -----------> c ---------------+
85 --                            \
86 --                             \
87 --                              +--------------- e -->
88
89 dag = nn.DAG()
90
91 a = nn.Linear(50, 10)
92 b = nn.ReLU()
93 c = nn.Linear(10, 15)
94 d = nn.CMulTable()
95 e = nn.Mul(-1)
96
97 dag:connect(a, b, c)
98 dag:connect(b, nn.Linear(10, 15), nn.ReLU(), d)
99 dag:connect(c, d)
100 dag:connect(c, e)
101
102 dag:setLabel(a, 'first module')
103
104 dag:setInput(a)
105 dag:setOutput({ d, e })
106
107 -- Check the output of the dot file. Generate a pdf with:
108 --
109 -- dot ./graph.dot -Lg -T pdf -o ./graph.pdf
110 --
111 print('Writing ./graph.dot')
112 dag:saveDot('./graph.dot')
113
114 -- Let's make a model where the dag is inside another nn.Container.
115 model = nn.Sequential()
116    :add(nn.Linear(50, 50))
117    :add(dag)
118    :add(nn.CAddTable())
119
120 criterion = nn.MSECriterion()
121
122 if cunn then
123    print("Using CUDA")
124    model:cuda()
125    criterion:cuda()
126    torch.setdefaulttensortype('torch.CudaTensor')
127    epsilon = 1e-3
128 end
129
130 local input = torch.Tensor(30, 50):uniform()
131 local output = model:updateOutput(input):clone()
132 output:uniform()
133
134 -- Check that DAG:accGradParameters and friends work okay
135 print('Gradient estimate error ' .. checkGrad(model, criterion, input, output, epsilon))
136
137 -- Check that we can save and reload the model
138 model:clearState()
139 torch.save('./test.t7', model)
140 local otherModel = torch.load('./test.t7')
141 print('Gradient estimate error ' .. checkGrad(otherModel, criterion, input, output, epsilon))
142
143 dag:print()