仅CPU环境运行PyTorch时[WinError 127]错误的解决方法
无GPU环境下运行CodWOE模型的解决步骤
彻底清理CUDA相关Torch残留
先完全卸载现有PyTorch相关包:# Conda环境 conda uninstall torch torchvision torchaudio -y # Pip环境 pip uninstall torch torchvision torchaudio -y手动删除Anaconda环境目录下的
Lib\site-packages\torch文件夹,再清理缓存:pip cache purge conda clean -a -y重新安装纯CPU版PyTorch
推荐安装兼容性更好的PyTorch 2.x纯CPU版本:pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu若坚持使用1.8.1版本,执行:
pip install torch==1.8.1+cpu torchvision==0.9.1+cpu torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html安装完成后验证:运行
import torch; print(torch.cuda.is_available()),输出应为False。强制模型使用CPU运行
- 遍历代码中所有设备指定逻辑,直接替换为
device = torch.device('cpu'),删除所有cuda相关的条件判断 - 模型加载时添加
map_location='cpu'参数,示例:model = torch.load('model_path.pth', map_location='cpu') - 注释或删除所有涉及CUDA的操作代码,比如
torch.cuda.empty_cache()
- 遍历代码中所有设备指定逻辑,直接替换为
排查依赖冲突
用pip list查看环境包列表,确保torch、torchvision、torchaudio均为带+cpu后缀的版本;若存在其他依赖包强制引入CUDA版Torch,需卸载后重新安装其CPU兼容版本。
内容的提问来源于stack exchange,提问作者user11652533
相关产品推荐
相关产品推荐

