x = torch.tensor([6., 9.], requires_grad=True)
y = torch.tensor([0.1, 0.01], requires_grad=True)
y_clone = y.clone()
z = torch.sum(x * y_clone + y_clone)
z.sum().backward()
print(x.grad)
print(y.grad)
print(y_clone.grad)
will give
tensor([0.1000, 0.0100])
tensor([ 7., 10.])
None
I am not sure why y_clone.grad is None, but I know that clone will still pass the gradient to y. Since it is a clone, so the working on gradient flow for y_clone is the same as y. I expect the y_clone.grad will be the same as y.grad but not.
x = torch.tensor([6., 9.], requires_grad=True)
y = torch.tensor([0.1, 0.01], requires_grad=True)
y_clone = y.clone()
y_clone[0] = 2.99
z = torch.sum(x * y_clone + y_clone)
z.sum().backward()
print(x.grad)
print(y.grad)
print(y_clone.grad)
will give
tensor([2.9900, 0.0100])
tensor([ 0., 10.])
None
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:9: UserWarning: The .grad attribute of a Tensor that is not a leaf Tensor is being accessed. Its .grad attribute won't be populated during autograd.backward(). If you indeed want the gradient for a non-leaf Tensor, use .retain_grad() on the non-leaf Tensor. If you access the non-leaf Tensor by mistake, make sure you access the leaf Tensor instead. See github.com/pytorch/pytorch/pull/30531 for more information.
if __name__ == '__main__':
I have no idea how to interpret this.
Thanks!