【问题标题】:pytorch running: RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpupytorch running: RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 和 cpu
【发布时间】:2021-03-03 21:08:19
【问题描述】:

运行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))

谁能知道原因并帮我解决它!谢谢!

【问题讨论】:

  • 您正在尝试对 2 个张量求和,一个在 CPU(您使用 torch.eye 定义的单位矩阵)上,另一个在 GPU (feats) 上。因此,在第 44 行添加 .to('cuda') 如下:torch.eye(feats.size(1)).to('cuda')

标签: python-3.x pytorch


【解决方案1】:

张量 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()

【讨论】:

  • 谢谢,torch.FloatTensor([n]) 出现同样的错误。更改为 torch.FloatTensor([n]).cuda() 修复了它。
猜你喜欢
  • 1970-01-01
  • 2021-04-27
  • 2021-12-24
  • 2021-07-18
  • 2021-05-11
  • 2022-01-03
  • 2020-03-24
  • 2021-03-26
  • 2020-11-14
相关资源
最近更新 更多