pytorch running: RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu

Viewed 11130

When I running the python code, there is a runtimeError in line 44:RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

    42  feats = self.node_features[self.train_mask]
    43  labels = self.node_labels[train_mask]

    44  A = torch.mm(feats.t(), feats) + 1e-05 * torch.eye(feats.size(1))

    45  labels_one_hot = torch.zeros((feats.size(0), self.n_classes))

Can anyone who knows the reason and help me fix it! Thanks!

1 Answers

It seems that the tensor torch.eye(...) is on CPU. you need to pass it as -

44  A = torch.mm(feats.t(), feats) + 1e-05 * torch.eye(feats.size(1)).to(device='cuda')

or

44  A = torch.mm(feats.t(), feats) + 1e-05 * torch.eye(feats.size(1)).cuda()
Related