简体   繁体   中英

Cannot transfer torch.Tensor to torch.cuda.Tensor

I use the following code to turn the torch.Tensor into torch.cuda.Tensor

import torch
import numpy as np

a = np.random.randn(1, 1, 2, 3)

t2 = torch.tensor(a)
t2 = t2.cuda()

t3 = torch.tensor(a, device=torch.device('cuda'))
print(type(t3), t3.device, type(t2), t2.device)

And its output is <class 'torch.Tensor'> cuda:0 <class 'torch.Tensor'> cuda:0

I expect the class is 'torch.cuda.Tensor'. I don't know why this happens. My torch version is 1.6.0 and the corresponding cuda version is 10.2

That is because there is no such class as torch.cuda.Tensor in PyTorch 1.6.0.

The t3.device and t2.device in print(type(t3), t3.device, type(t2), t2.device) prints cuda:0 which means your tensors are already on GPU.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM