Is it possible to get the gradient w.r.t. a subset of coordinates by indexing the parameter vector ?
For example, I was hoping that the code below would give me the gradients w.r.t. the first 2 coordinates of “x” instead of the whole “x” vector, but it gives me:
*** RuntimeError: differentiated input is unreachable
which tells me (correct me if I am wrong) that the indexing is returning a copy of the variable instead of a reference.
import torch
from torch.autograd import Variable

x = Variable(torch.randn(1,5), requires_grad = True)
y = Variable(torch.zeros(1))

loss = 0.5*((x - y)**2).sum()

# Runtime Error occurs here
grad = torch.autograd.grad(loss, x[0,:2], create_graph=True)[0]