Skip to content

Instantly share code, notes, and snippets.

@bartvm
Created May 27, 2016 16:23
Show Gist options
  • Save bartvm/0c89afae109f3e08958f5914c6148118 to your computer and use it in GitHub Desktop.
Save bartvm/0c89afae109f3e08958f5914c6148118 to your computer and use it in GitHub Desktop.
grad = require 'autograd'
torch = require 'torch'
params={
W=torch.range(0, 8):view(3, 3),
storage=torch.zeros(3, 3)
}
function f(params, x)
params.storage[2] = params.W * x
return torch.mean(params.storage)
end
grad, _ = grad(f)(params, torch.ones(3))
print(grad.W)
print(grad.storage)
--[[
This gives a zero gradient to W, which is incorrect.
0 0 0
0 0 0
0 0 0
[torch.DoubleTensor of size 3x3]
0.1111 0.1111 0.1111
0.0000 0.0000 0.0000
0.1111 0.1111 0.1111
[torch.DoubleTensor of size 3x3]
--]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment