Hello, everyone! I want to calculate per-example gradients. But when I run the code snippet (linear regression to 0) below, I got RuntimeError: invalid gradient at index 0 - got [10] but expected shape compatible with [100].
n_sample = 100
n_feature = 10
A=torch.randn(n_sample, n_feature)
x=torch.randn(n_feature)
grad_tensors = [x] * n_sample
b=torch.matmul(A,x)
losses = b ** 2
losses.requires_grad = True

print(losses.size())
torch.autograd.backward(tensors=(losses, ), grad_tensors=grad_tensors)
print(grad_tensors)


losses.size()=torch.Size([100]), i.e, the number of samples. But why does it expect shape compatible with [100].