ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

PyTorch Geometric PGExplainer报 Expected all tensors to be on the same device 错误?三步把解释器挪到同设备

PyTorch Geometric PGExplainer报 Expected all tensors to be on the same device 错误?三步把解释器挪到同设备 PyTorch Geometric PGExplainer报 Expected all tensors to be on the same device 错误三步把解释器挪到同设备【免费下载链接】pytorch_geometricGraph Neural Network Library for PyTorch项目地址: https://gitcode.com/GitHub_Trending/py/pytorch_geometric你已经把训练好的 GNN 模型挪到了 GPU却在 PyTorch Geometric 里一调用图解释器 PGExplainer 的train()就抛出RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!。这个报错到底在抱怨什么先确认你是不是也卡在这里报错文本包含Expected all tensors to be on the same device调用栈指向 pg_explainer.py 的train或其内部 MLP 前向你的模型、x、edge_index都在cuda:0上纯 CPU 环境复现不了只有模型上了 GPU 才炸你正在用循环调用explainer.algorithm.train(...)训练解释器——PGExplainer 必须先训练若干轮epochs才能生成边掩码edge mask即哪些边对预测起关键作用的打分为什么会这样一个类比讲清楚把 PGExplainer 想象成一家掩码生成车间它内部有个小 MLP 工人负责给图的每条边打分。问题在于这位工人是在构造函数里招进来的没指定工作地点默认被编到了 CPU 车间而你要解释的 GNN 老员工一直在 GPU 车间干活。train()的流水线是先从模型里取出节点嵌入node embedding模型对每个节点的内部表征——它们在 GPU 上然后把这些嵌入喂给 MLP 给每条边打掩码分。CPU 车间的工人伸手去拿 GPU 传送带上的料PyTorch 直接喊停于是抛出所有张量必须在同一设备的报错。看源码里这段关键逻辑就明白了改写自构造函数self.mlp Sequential( Linear(-1, 64), ReLU(), Linear(64, 1), ) # ← 没有任何设备参数默认全部落在 CPU 上所以根因就一句话解释器的 MLP 参数和你的输入张量分处两个设备。好消息是 PGExplainer 本身是个nn.Module随时可以用.to(device)整体搬过去源码细节见 pg_explainer.py。动手修按你的场景选一条路先搬解释器再开训最直接的修复适用一次性脚本或 notebook只想立刻让训练循环跑起来。操作把模型、数据、解释器三件套统一锁到同一个devicedevice torch.device(cuda if torch.cuda.is_available() else cpu) model Net().to(device) # GNN 模型上 GPU data data.to(device) # 特征 x 和 edge_index 同设备 algorithm PGExplainer(epochs30, lr0.003) algorithm.to(device) # ← 关键行解释器整体搬过去 for epoch in range(30): for index in node_indices: # 逐个节点训练掩码 loss algorithm.train(epoch, model, data.x, data.edge_index, targetdata.y, indexindex)确认生效打印next(algorithm.mlp.parameters()).device输出cuda:0且与next(model.parameters()).device一致第一轮train不再抛错即修好。异构图逐键搬运后同样挪解释器适用你解释的是异构图x和edge_index是字典每条边类型各有一份张量。操作字典数据要逐键搬解释器挪法不变d next(model.parameters()).device # ← 以模型所在设备为准 data.x {k: v.to(d) for k, v in data.x.items()} data.edge_index {k: v.to(d) for k, v in data.edge_index.items()} data.y data.y.to(d) algorithm PGExplainer(epochs30, lr0.003).to(d) # 链式 .to 一步到位确认生效任选一个边类型打印data.edge_index[et].device与next(algorithm.mlp.parameters()).device两者相同跑完 1 个 epoch 不报错。长项目加一道开训前的设备体检适用多人协作的仓库想在问题复发前就被拦下。操作写个三行体检函数每次开训先过一遍def check_devices(model, algorithm, data): d next(model.parameters()).device assert next(algorithm.mlp.parameters()).device d, 解释器没搬设备 assert data.x.device d and data.edge_index.device d, 数据还在 CPU确认生效check_devices(model, algorithm, data)静默通过一旦有人漏搬立刻给出带原因信息的断言而不是跑到第 30 轮才炸出设备错。别踩这些坑⚠️ 别只想着model.to(device)和data.to(device)却忘了解释器——它带着自己的 MLP 参数不搬照样炸别把cuda:0直接写死在脚本里用torch.cuda.is_available()兜底换台没 GPU 的机器就起不来别跳过训练循环直接调explainer(...)要解释结果——会报 not yet fully trained那是没训练够轮数的报错不是设备问题别被误导去反复改设备下一步解释器接口的完整参数见 pg_explainer.py官方带 CUDA 测试的完整用法可参考 test_pg_explainer.py。把解释器加上.to(device)后重新跑一遍你刚才的复现脚本train循环应该能顺畅跑完 30 轮并输出掩码了。【免费下载链接】pytorch_geometricGraph Neural Network Library for PyTorch项目地址: https://gitcode.com/GitHub_Trending/py/pytorch_geometric创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表