python-3.x - 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))
哪位知道原因的可以帮我解决一下!谢谢!
解决方案
似乎张量 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()
推荐阅读
- javascript - 如何在javascript中动态添加“selected”属性以选择选项?
- apache-spark - 无法在 databricks 社区版集群中创建 dbfs 文件。FileNotFoundError: [Errno 2] 没有这样的文件或目录:
- android - Android 4.0.1 - EditText 中的 XML 呈现错误。索引:0,大小:0 - Vew 绘制失败
- bash - 对同一行上的数字进行排序,删除重复项但保持原始
- python - 熊猫数据框中显示不存在的列
- html - Why does setting min-width or min-height to 0 prevent CSS flex and grid overflow?
- r - get browsing state in a function
- r - R函数将代码返回到R Studio中的源代码编辑器?
- node.js - npm 命令期间的 Angular-cli 错误
- flutter - 如何使用 Flutter 创建以下过渡效果?