5 Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/
6 Written by Francois Fleuret <francois.fleuret@idiap.ch>
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.
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.
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/>.
27 function checkGrad(model, criterion, input, target)
28 local params, gradParams = model:getParameters()
32 local output = model:forward(input)
33 local loss = criterion:forward(output, target)
34 local gradOutput = criterion:backward(output, target)
36 model:backward(input, gradOutput)
37 local analyticalGradParam = gradParams:clone()
39 for i = 1, params:size(1) do
42 params[i] = x - epsilon
43 local output0 = model:forward(input)
44 local loss0 = criterion:forward(output0, target)
46 params[i] = x + epsilon
47 local output1 = model:forward(input)
48 local loss1 = criterion:forward(output1, target)
52 local ana = analyticalGradParam[i]
53 local num = (loss1 - loss0) / (2 * epsilon)
59 err = torch.abs(num - ana) / torch.abs(num)
66 .. ' analytical ' .. ana
67 .. ' numerical ' .. num
73 function printTensorTable(t)
74 if torch.type(t) == 'table' then
75 for i, t in pairs(t) do
76 print('-- ELEMENT [' .. i .. '] --')
84 -- torch.setnumthreads(params.nbThreads)
85 torch.setdefaulttensortype('torch.DoubleTensor')
91 -- input --> a --> b ---> d ----+ g --> output
118 input = torch.Tensor(3, 10):uniform()
120 print('******************************************************************')
121 print('** updateOutput **************************************************')
122 print('******************************************************************')
124 output = model:updateOutput(input):clone()
126 printTensorTable(output)
128 print('******************************************************************')
129 print('** updateGradInput ***********************************************')
130 print('******************************************************************')
132 gradInput = model:updateGradInput(input, output)
134 printTensorTable(gradInput)
136 print('******************************************************************')
137 print('** checkGrad *****************************************************')
138 print('******************************************************************')
142 checkGrad(model, nn.MSECriterion(), input, output)