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

Commit

Permalink
Fix zero gradient for subtensor assignment.
Browse files Browse the repository at this point in the history
The variable that is being assigned has its gradient correctly
calculated (g[k]) but later on when the gradient of the variable being
assigned to is calculated g[k] is set to 0. This gives the correct
gradient for the variable being assigned to, but because it shares the
same storage it actually overrides the earlier gradient incorrectly to
zero. This fixes that.
  • Loading branch information
bartvm committed May 27, 2016
1 parent ff0057f commit 0d31b91
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/gradfuns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,12 @@ functions.set = {
return nil
end,
function(g, ans, x, k, v)
return g[k]
local gk = getValue(g[k])
if type(gk) == 'number' then
return gk
else
return torch.clone(gk)
end
end,
}

Expand Down
5 changes: 5 additions & 0 deletions test/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,11 @@ local tests = {
return torch.sum(xc)
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)
end
tester:assert(gradcheck(f5,{x=torch.randn(10,10),y=torch.randn(10)}), "Incorrect gradient")
end,

ScalarSigmoid = function()
Expand Down

0 comments on commit 0d31b91

Please sign in to comment.