方法一:
#name替换为自己的名字就好
conda create -n name python=3.6.6 #创建虚拟环境,python版本自己决定
conda activate name#进入创建的虚拟环境
conda install pytorch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 cpuonly #这是cpu版本的,中间的版本也可以不用指定,conda会自己适配
conda install scikit-learn #可能会用到
##gpu版本的装载略微复杂一点,进官网PyTorch
如果出现下载慢的问题或者报错(e.g.runtimeError),可以试试添加源,步骤如下:
查看已安装的源
conda config --show channels
添加源
conda config --add channels #在这后面添加需要的源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
方法二:
找一个已经下载好的pytorch的电脑
离线下载环境安装包,将机器A上的anaconda3/envs目录下对应的环境(找到有pytorch的虚拟环境)拷贝到机器B上对应的目录。
找到anaconda3/envs文件夹。例如上面name取名为pytroch_cpu,则直接将pytroch_cpu拷贝到该目录下。进入anaconda,运行命令行conda activate pytorch_cpu。检测是否移植成功。
验证
如果用pycharm等IDE,需要在文件(file)——设置(setting)——项目(project)——解释器(interpreter)将python解释器换为自己上面刚建的虚拟环境
如果下拉没找到,需要自行手动添加,找到右侧的加号,或者是设置,添加,或者ADD,
点击conda环境,点击现有环境,解释器,右侧省略号,找到对应的虚拟环境的位置,比如下图,name为labelme,或者pytorch3d的解释器
终于可以验证了
import torch
#返回Ture说明cuda可用
print(torch.cuda.is_available())
#查看可行的cuda数目
print(torch.duda.device_count())
如果直接使用Anaconda Prompt
按顺序一条一条运行
conda activate name
python
import torch
torch.cuda.is_available()
torch.cuda.device_count()