RuntimeError: Expected all tensors to be on the same device, but found at least two devices

当运行PyTorch代码时遇到RuntimeError,是因为参与运算的张量在不同的设备上,如CPU和GPU。解决方法是将所有变量统一到同一设备,例如使用torch.device将模型和数据转移到GPU或CPU。示例代码展示了如何根据CUDA是否可用将模型和数据移动到GPU。
摘要由CSDN通过智能技术生成

问题:RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument mat2 in method wrapper_mm)

在这里插入图片描述

原因

参与运算的两个或多个变量,有的在CPU上,有的在GPU上

解决方案

若在gpu上运行,则把所有变量都放在gpu上
如:

device=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model=GCN_Net(dataset.num_node_features,16,dataset.num_classes).to(device)
data=dataset[0].to(device)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

筱文rr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值