Hi. In my code I have a list x that I have to convert to a tensor. However, when I do:
x=torch.tensor(x)
I get the following warning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
I saw this same error online being asked before but for some reason I’m unable to change the code so the warning goes away. What code would I have to substitute instead of x=torch.tensor(x) to resolve this warning? Thanks!
The warning is raised if x
is already a tensor as seen here:
x = torch.randn(10)
x = torch.tensor(x)
# UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
x = [0, 1, 2, 3]
x = torch.tensor(x) # no warning
In this case you won’t need to pass it again into torch.tensor
and could clone
it instead if needed.