> 当前colab中pytorch的版本是1.4.0,pytorchvision的版本是0.5..python版本3.6,CUDA版本是10
自己下载了一份代码,需要的pytorch环境是0.4.1,在没配置pytorch环境时,代码中在
import torch.nn._functions
是报错没有此模块,搜索后是因为pytorch>=1.0版本后,没有了该模块,并且也没找到新的import方法。于是需要配置pytorch0.4.1环境。下面是踩坑无数次后得到的配置流程:
1. 卸载pytorch并重新安装
新建一个colab笔记本,
!pip3 install http://download.pytorch.org/whl/cu80/torch-0.4.1-cp36-cp36m-linux_x86_64.whl
结果:
可以使用如下代码检查:
import torch
print(torch.__version__)
结果:0.4.1
此时可以看到图片中,没有安装合适的torchvision版本,需要手动安装
2. 安装pytorchvision
!pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ torchvision==0.2.1.post2
结果:
此时,在import torchvision时还会报错:显示无'PILLOW_VERSION',原因是该模块在torchvision0.5后就去掉了,对于老版本的torchvision,需要重新安装。
需要运行以下代码:
!pip uninstall pillow
!pip install pillow==5.2.0
此时再import torchvision就没有问题了!
--------
遇到的其他报错:
module 'torch.nn' has no attribute 'ModuleDict'
原因是之前先安装了torch0.4.0的版本,安装0.4.1后就不会有这个问题。