首页 > 解决方案 > pytorch running: RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 和 cpu

问题描述

当我运行python代码时,第44行出现runtimeError: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))

哪位知道原因的可以帮我解决一下!谢谢!

标签: python-3.xpytorch

解决方案


似乎张量 torch.eye(...) 在 CPU 上。您需要将其传递为-

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

或者

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

推荐阅读