RuntimeError: indices should be either on cpu or on the same device as the indexed tensor (cpu)

博主在训练图神经网络时遇到了GPU资源占用但依然报错的问题。经过检查代码和模型配置,发现是torch和PyG版本不兼容导致。通过创建新的conda环境并降级torch和PyG到特定版本,问题得到解决。具体操作包括复制当前环境,然后使用pip卸载并安装指定版本的torch和PyG相关库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 训练图神经网络时遇到的问题:

    检查过自己的代码,模型和数据都分别放入了GPU中,将数据和模型打印出来也在GPU上、运行时GPU的显存确实有被占但依然报这个错误。

  2. 解决的办法

    最终解决的办法是conda此时的环境复制一份,在复制的环境上将torch和pyg降级。
    conda复制:conda create -n new-env --clone old-env
    我的pytorch版本1.13.0-cuda117,更换为了torch==1.10.1+cu113
    pyg也更换为了对应版本

  3. 命令

# torch
pip uninstall torch torchvision torchaudio
pip install torch==1.10.1+cu113 torchvision==0.11.2+cu113 torchaudio==0.10.1 -f https://download.pytorch.org/whl/cu113/torch_stable.html
# pyg
pip uninstall torch-scatter torch-sparse torch-geometric torch-cluster torch-spline-conv
pip install torch-scatter -f https://data.pyg.org/whl/torch-1.10.0+cu113.html
pip install torch-sparse -f https://data.pyg.org/whl/torch-1.10.0+cu113.html
pip install torch-geometric
pip install torch-cluster -f https://data.pyg.org/whl/torch-1.10.0+cu113.html
pip install torch-spline-conv -f https://data.pyg.org/whl/torch-1.10.0+cu113.html
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值