Clarification - Using backward() on non-scalars
Hey, Would anyone be able to point me in the direction of an example if one exists? If not, say I had the following super simple example: x = Variable(torch.FloatTensor([[2,1]]), requires_grad=True) M = Variable(torch.FloatTensor([[1,2],[3,4]])) y = torch.mm(x,M)
Mattie
<SCODE>x = Variable(torch.FloatTensor([[2,1]]), requires_grad=True)
M = Variable(torch.FloatTensor([[1,2],[3,4]]))
y = torch.mm(x, M)
jacobian = torch.FloatTensor(2, 2).zero_()
y.backward(torch.FloatTensor([[1, 0]]), retain_variables=True)
jacobian[:,0] = x.grad.data
x.grad.data.zero_()
y.backward(torch.FloatTensor([[0, 1]]), retain_variables=True)
jacobian[:,1] = x.grad.data
<ECODE> torch.autograd.backward([y], [torch.FloatTensor([[1, 0]])], retain_variables=True)
colesbury
Fab, that’s a great help. Thanks for your time.
Mattie
I am a bit confused by this. In this example, isn’t that the “loss” is y? Is that correct? Thanks a lot!
linlin
is used instead of Thank you in advance!
jsm
jdhao
jsm
jdhao
linlin
yottabytt
jdhao
saan77
Jolyon
Huimin_ZENG