I have created a DataLoader that looks like this
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
I am using this data loader like so
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()
This fails with the message
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
For the return command inside ToTensor(), in fact any attempt to move the tensor te the GPU will fail inside that class. I have tried:
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))
inside the body of __call__ in ToTensor() and it fails with the same message, whereas it succeeds everywhere else.
Why is this error generated and how can I resolve this?
import torchx = torch.randn(2, 2, device='cuda:0')print(x)X = torch.from_numpy(X).cuda("cuda:0")deviceunfortunately.