Converting a list of lists and scalars to a list of PyTorch tensors throws warning
I was converting a list of lists to a PyTorch tensor and got a warning message. The conversion itself isn't difficult. For example:
&gt;&gt;&gt; import torch
&gt;&gt;&gt; thing = [[1, 2, 3, 4, 5], [2, 3], 2, 3]
&gt;&gt;&gt; thing_tensor = list(map(torch.tensor, thing))

I get the warning:
home/user1/files/module.py:1: 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).

and am wondering what the reason may be. Is there any other way to convert the data into a tensor that I'm not aware of? Thanks.

I tried to reproduce your warning but failed to do so. However, I could get the same warning by creating if I replaced the lists in thing by tensors.
I'll go over why it is better to use x.clone().detach() rather than torch.tensor(x) to make a copy :
On my version of pytorch using torch.tensor will work to create a copy that is no longer related to the computational graph and that does not occupy the same place in memory. However, this behaviour could change in future versions which is why you should use a command that will remain valid. I'll illustrate problems that come with note being detached or occupying the same spot in the memory.
Not being detached :
x = torch.tensor([0.],requires_grad=True)
y = x.clone()
y[0] = 1
z = 2 * y
z.backward()
print(x, x.grad)


tensor([0.], requires_grad=True) tensor([0.])

As you can see the gradient of x is being updated while the computation is done on y, but changing the value of y won't change the value of x because they don't occupy the same memory space.
Occupying the same memory space :
x = torch.tensor([0.],requires_grad=True)
y = x.detach().requires_grad_(True)
z = 2 * y
z.backward()
y[0] = 1
print(x, x.grad)


tensor([1.], requires_grad=True) None

In this case, the gradients are not updated but changing the value of y changes the value of x because they occupy the same memory space.
Best practice :
As suggested by the warning, the best practice is to both detach and clone the tensor :
x = torch.tensor([0.],requires_grad=True)
y = x.clone().detach().requires_grad_(True)
z = 2 * y
z.backward()
y[0] = 1
print(x, x.grad)


tensor([0.], requires_grad=True) None

This ensures that future modifications and computations from y won't affect x
