[英]Cannot transfer torch.Tensor to torch.cuda.Tensor
I use the following code to turn the torch.Tensor into torch.cuda.Tensor我使用以下代码将 torch.Tensor 变成 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
它的 output 是
<class 'torch.Tensor'> cuda:0 <class 'torch.Tensor'> cuda:0
I expect the class is 'torch.cuda.Tensor'.我希望 class 是“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
我的torch版本是1.6.0对应的cuda版本是10.2
That is because there is no such class as torch.cuda.Tensor
in PyTorch 1.6.0.那是因为
torch.cuda.Tensor
1.6.0中没有torch.cuda.Tensor这样的class。
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. print(type(t3), t3.device, type(t2), t2.device)
中的t3.device
和t2.device
打印cuda:0
这意味着您的张量已经在 GPU 上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.