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?