albanD:

res.sum().backward()


Hi, thank you for your reply, but, sorry, I still didn’t get it.
Please take a look at following examples:
>>> x = torch.randn(2,2,requires_grad=True)
>>> y = x.clone()
>>> res=y.sum()
>>> res.backward()
>>> y.grad
>>> x.grad
tensor([[1., 1.],
        [1., 1.]])

>>> x = torch.randn(2,2,requires_grad=True)
>>> y = torch.randn(2,2)
>>> y.copy_(x)
tensor([[ 0.4119, -0.7538],
        [-0.3020, -0.6225]], grad_fn=<CopyBackwards>)
>>> res = y.sum()
>>> res.backward()
>>> y.grad
>>> x.grad
tensor([[1., 1.],
        [1., 1.]])

As I see it, the two operations behave the same if used separately.
In your example, there were three tensors (x, y, and res) and you used .clone().copy_(x) together. To be honest, I got more confused. Could you explain the reason why did you use them together?
And what’s the difference between >>y.grad >>nothing printed out and >>y.grad >>a tensor of zeros printed out?
Thank you for your time!