错误
好久没玩torch了,直接装了个最新版本的torch2.0.1+cu117版本, python是3.9
之后在运行代码的时候出现了以下的错误
RuntimeError: GET was unable to find an engine to execute this computation
如果要是cpu跑没有任何报错,但是如果是gpu跑的话就会出现如上错误
class CNNModel(nn.Module):def __init__(self, input_size, num_classes):super(CNNModel, self).__init__()self.conv = nn.Sequential(nn.Conv1d(in_channels=input_size, out_channels=32, kernel_size=3, padding=1),nn.ReLU(),nn.Conv1d(in_channels=32, out_channels=64, kernel_size=3, padding=1),nn.ReLU(),nn.Conv1d(in_channels=64, out_channels=128, kernel_size=3, padding=1),nn.ReLU(),)self.fc1 = nn.Sequential(nn.Linear(128, 64),nn.ReLU(),nn.Dropout(0.5),)self.fc2 = nn.Linear(64, num_classes)def forward(self, x):out = self.conv(x)out = torch.flatten(out, start_dim=1)out = self.fc1(out)out = self.fc2(out)return outx = torch.zeros(64, 128, 1).cuda()
model = CNNModel(input_size=128, num_classes=6)
model.cuda()
model(x)
跑这个代码
![c101423b56aa03469b5d43310a630aa1.png](https://img-blog.csdnimg.cn/img_convert/c101423b56aa03469b5d43310a630aa1.png)
解决方案
我之前写的大多都是torch1.x的操作
torch2.0 看文档 主要是提升了模型运行速度的能力(针对大模型,新增了torch.compile能力),可以向下兼容,但是我发现很多人在使用过程中都出现了这个问题,具体原因还没有搞清楚,主要出现在conv1d的环节,lstm正常。
解决方案就是把目前这个torch卸载掉,安装以前的版本
我这里使用的依旧是anaconda,我建议直接把虚拟环境删掉,重新建立,因为torch卸载有的时候不干净,安装新的torch会有各种冲突,很麻烦。
假设我门目前使用的虚拟环境时new_env1
# 删掉该环境(谨慎点,如果不方便可以先不删,重新建立新的环境)
conda remove -n new_env1 --all
# 创建新的虚拟环境
conda create -n new_env2 python=3.9
因为torch torchvision cuda以及python版本都是一一有兼容关系的,所以不要乱安装,并且直接安装都时cpu版本,还得再安装对应版本的cuda,更麻烦。
最好这样,直接一个命令搞定
查看:https://pytorch.org/get-started/previous-versions/
亲测有效:conda安装一般都是不好使的,使用pip安装
比如安装torch1.9.0
pip install torch==1.9.0+cu111 torchvision==0.10.0+cu111 torchaudio==0.9.0 -f https://download.pytorch.org/whl/torch_stable.html
在运行就正常了。
推荐阅读:
我的2022届互联网校招分享
我的2021总结
浅谈算法岗和开发岗的区别
互联网校招研发薪资汇总
2022届互联网求职现状,金9银10快变成铜9铁10!!
公众号:AI蜗牛车
保持谦逊、保持自律、保持进步
发送【蜗牛】获取一份《手把手AI项目》(AI蜗牛车著)
发送【1222】获取一份不错的leetcode刷题笔记
发送【AI四大名著】获取四本经典AI电子书