Can not move the tensor onto GPU

Hi everyone, I am using PyTorch 1.7 and cuda 10.2, I found a strange thing, please see the following code and corresponding outputs:

test =torch.randn(3,3)
print(test.device)
test.to('cuda:0')
print(test.device)

test_2 = torch.randn(3,3,device='cuda:0')
print(test_2.device)

which has outputs:

cpu
cpu
cuda:0

It seems strange to me, since I can set its location onto gpu when initialize the tensor but cannot move it onto gpu if it was initialized on cpu, can anyone help me with this? Thanks in advance!

What you want:

device = torch.device(‘cuda:0’)
test = test.to(device)

Thanks Kirondem! I just realized I forget to reassign the variable. (actually a little silly of me :slight_smile:

you have to pass additional argument device = cuda in torch.randn()