Why Tensor.clone().detach() is recommended when copying a tensor?

Viewed 5685

What is the difference between copying a tensor using torch.tensor(sourcetensor) and tensor.clone().detach()?


Like torch.tensor(srctsr) always copies data, tensor.clone().detach() copies data too.

x = torch.tensor([1, 2, 3])
y1 = x.clone().detach()
y2 = torch.tensor(x)
x[0] = 0
print(y1, y2) # both are same

So they seem to be exactly the same. Below is the explanation given in the PyTorch documentation about torch.tensor() and torch.clone().detach()

Therefore torch.tensor(x) is equivalent to x.clone().detach() and torch.tensor(x, requires_grad=True) is equivalent to x.clone().detach().requires_grad_(True). The equivalents using clone() and detach() are recommended.

So if they are equivalent to each other, why is .clone().detach() more preferred than the other?

1 Answers

The difference is described here. I am adding some text (from the link) for the sake of completeness.

  • torch.tensor() always copies data. If you have a Tensor data and want to avoid a copy, use torch.Tensor.requires_grad_() or torch.Tensor.detach().

  • When data is a tensor x, torch.tensor() reads out ‘the data’ from whatever it is passed, and constructs a leaf variable. Therefore torch.tensor(x) is equivalent to x.clone().detach() and torch.tensor(x, requires_grad=True) is equivalent to x.clone().detach().requires_grad_(True). The equivalents using clone() and detach() are recommended.

Related