将NumPy数组正确转换为在GPU上运行的PyTorch张量

时间:2019-02-19 19:06:35

标签: python numpy pytorch

我创建了一个DataLoader,看起来像这样

class ToTensor(object):
    def __call__(self, sample):
        return torch.from_numpy(sample).to(device)

class MyDataset(Dataset):
    def __init__(self, data, transform=None):
        self.data = data
        self.transform = transform

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        sample = self.data[idx, :]

        if self.transform:
            sample = self.transform(sample)

        return sample

我正在像这样使用此数据加载器

dataset = MLBDataset(
        data=data,
        transform=transforms.Compose([
            ToTensor()
        ]))
dataloader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=4)
dataiter = iter(dataloader)
x = dataiter.next()

这失败并显示消息

THCudaCheck FAIL file=/opt/conda/conda-bld/pytorch_1549628766161/work/aten/src/THC/THCGeneral.cpp line=55 error=3 : initialization error
THCudaCheck FAIL file=/opt/conda/conda-bld/pytorch_1549628766161/work/aten/src/THC/THCGeneral.cpp line=55 error=3 : initialization error
THCudaCheck FAIL file=/opt/conda/conda-bld/pytorch_1549628766161/work/aten/src/THC/THCGeneral.cpp line=55 error=3 : initialization error
THCudaCheck FAIL file=/opt/conda/conda-bld/pytorch_1549628766161/work/aten/src/THC/THCGeneral.cpp line=55 error=3 : initialization error
...
    torch._C._cuda_init()
RuntimeError: cuda runtime error (3) : initialization error at /opt/conda/conda-bld/pytorch_1549628766161/work/aten/src/THC/THCGeneral.cpp:55

对于return中的ToTensor()命令,实际上,任何在GPU中移动张量的尝试都将失败。我尝试过:

a = np.array([[[1, 2, 3, 4], [5, 6, 7, 8], [25, 26, 27, 28]],
             [[11, 12, np.nan, 14], [15, 16, 17, 18], [35, 36, 37, 38]]])
print(torch.from_numpy(a).to(device))

__call__ToTensor()的正文中,它以相同的消息失败,而在其他任何地方都成功。

为什么会产生此错误,我该如何解决?

2 个答案:

答案 0 :(得分:0)

尝试这个:

代码:

import numpy as np
import torch
import torch.nn as nn

torch.cuda.set_device(0)

X = np.ones((1, 10), dtype=np.float32)
print(type(X), X)
X = torch.from_numpy(X).cuda(0)
print(type(X), X)

model = nn.Linear(10, 10).cuda(0)
Y = model(X)
print(type(Y), Y)

输出:

<class 'numpy.ndarray'> [[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
<class 'torch.Tensor'> tensor([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]], device='cuda:0')
<class 'torch.Tensor'> tensor([[ 0.4867, -1.0050,  0.4872, -0.0260, -0.0788,  0.0161,  1.2210, -0.3957,
          0.2097,  0.2296]], device='cuda:0', grad_fn=<AddmmBackward>)

答案 1 :(得分:0)

根据link,这可能与多处理问题有关。您可以找到以下workaround