How to get cuda variable gradient?
why the result between CPU version and GPU version is different? How do we get grad for cuda variable? Thanks. CPU version: <SCODE>import torch
from torch.autograd import Variable

l = torch.nn.Linear(6,1)
input = Variable(torch.rand(10,6), requires_grad = True)
out = l(input)
target = Variable(torch.rand(10,1))
crt = torch.nn.L1Loss()
loss = crt(out, target)
loss.backward()
print input.grad
<ECODE> Output: GPU version: <SCODE>l = torch.nn.Linear(6,1).cuda()
input = Variable(torch.rand(10,6), requires_grad = True).cuda()
out = l(input)
target = Variable(torch.rand(10,1)).cuda()
crt = torch.nn.L1Loss().cuda()
loss = crt(out, target)
loss.backward()
print input.grad
<ECODE> Output: None
fxia22
The gradient does not work through .cuda(). So instead of use <SCODE>input = Variable(torch.rand(10,6).cuda(), requires_grad = True).
<ECODE> Best regards Thomas
tom
Thank you so much! -Fei
fxia22
I just stumbled into the same problem. Can somebody explain the logic behind it? Seems very counter-intuitive.
fabee
smth