Pytorch: layer not transferred on GPU with to() function
In the following code, I expected tensor x and layer l both on GPU, instead only the the tensor x results to be on the GPU, and not the layer l. In fact, using this approach results in RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! during a learning stage.
import torch
x = torch.zeros(1)
x = x.to('cuda')
try:
    x.get_device()
    print('x: gpu')
except:
    print('x:','cpu')

l = torch.nn.Linear(1,1)
l = l.to('cuda')
try:
    l.get_device()
    print('l: gpu')
except:
    print('l:','cpu')

the output is:
x: gpu
l: cpu

instead of both gpu.
why this?
Torch version: 1.10.2+cu113

you can't call .get_device() on a nn.Linear object, therefore your second try block fails and it prints the code in the exception part. In order to check what device your module is on you can do the following:
print(next(l.parameters()).device)

output:
&gt;&gt; device(type='cuda', index=0)

