import torch.nn as nn

 
x = torch.ones(1,10)
x.requires_grad = True

y = 2*torch.ones(1,10)
y.requires_grad = True

l1 = (y**2).mean()
l2 = (x**2).mean()
l = l1 +l2
l.backward()
print(x.grad)
print(y.grad)


x.grad = tensor([[0.2000, 0.2000, 0.2000, 0.2000, 0.2000, 0.2000, 0.2000, 0.2000, 0.2000,0.2000]])
ygrad = tensor([[0.4000, 0.4000, 0.4000, 0.4000, 0.4000, 0.4000, 0.4000, 0.4000, 0.4000,0.4000]])

it works .