model:backward(input, gradOutput)
local analyticalGradParam = gradParams:clone()
+ local err = 0
+
for i = 1, params:size(1) do
local x = params[i]
local ana = analyticalGradParam[i]
local num = (loss1 - loss0) / (2 * epsilon)
- local err
- if num == ana then
- err = 0
- else
- err = torch.abs(num - ana) / torch.abs(num)
+ if num ~= ana then
+ err = math.max(err, torch.abs(num - ana) / torch.abs(num))
end
-
- print(
- 'CHECK '
- .. err
- .. ' checkGrad ' .. i
- .. ' analytical ' .. ana
- .. ' numerical ' .. num
- )
end
+ return err
end
function printTensorTable(t)
output:uniform()
-checkGrad(model, nn.MSECriterion(), input, output)
+print('Error = ' .. checkGrad(model, nn.MSECriterion(), input, output))