Skip to content
This repository has been archived by the owner on Nov 1, 2021. It is now read-only.

Commit

Permalink
Add overwriting test, fix indexing test
Browse files Browse the repository at this point in the history
  • Loading branch information
bartvm committed Jul 13, 2016
1 parent db06c97 commit 1fa109a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/gradcheck.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-- Autograd
local autograd = require 'autograd'
local util = require 'autograd.util'

-- Perturbation (finite diffs):
local perturbation = 1e-6
Expand All @@ -12,20 +13,30 @@ local function jacobianFromAutograd(func, inputs, key)
-- Autograd:
local df = autograd(func)
local grads = df(table.unpack(inputs))
local gradsVerify = df(table.unpack(inputs))

-- Find grad:
local g = autograd.util.nestedGet(grads, key)
local g_clone
if torch.isTensor(g) then
g_clone = g:clone()
end

-- Get the grad again
local gradsVerify = df(table.unpack(inputs))
local gVerify = autograd.util.nestedGet(gradsVerify, key)
local err
local overwrite_err = 0
if torch.isTensor(g) then
err = (g - gVerify):abs():max()
overwrite_err = (g - g_clone):abs():max()
else
err = torch.abs(g - gVerify)
end

if err ~= 0 then
error("autograd gradient not deterministic")
elseif overwrite_err ~= 0 then
error("autograd gradient overwritten when called twice")
end

-- Return grads:
Expand Down
5 changes: 3 additions & 2 deletions test/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1688,8 +1688,9 @@ local tests = {
end
tester:assert(gradcheck(f4,{x=torch.randn(10,10),y=torch.randn(3)}), "Incorrect gradient")
local f5 = function(params)
params.x[2] = params.y*2.0
return torch.sum(params.x)
local xc = torch.clone(params.x)
xc[2] = params.y * 2.0
return torch.sum(xc)
end
tester:assert(gradcheck(f5,{x=torch.randn(10,10),y=torch.randn(10)}), "Incorrect gradient")
end,
Expand Down

0 comments on commit 1fa109a

Please sign in to comment.