本部分介绍pytorch环境的配置,包括CPU版本和GPU版本。
环境:win10+anaconda+python3.7
1.前置要求
安装完成anaconda后,在anaconda中创建一个虚拟环境以便管理。如:
conda create -n pytorch_learn python=3.7
2.Pytorch安装
pytorch传送门:PyTorch
如果熟悉自己的环境配置,请在官网主页直接选择自己的版本然后在anaconda prompt中输入命令进行安装。(注意要将自己需要配置的虚拟环境进行激活)
值得注意的是,笔者选用这种方式安装失败,主要在于这种方式是通过自带的pytorch仓库进行安装,可能会引发超时和各种玄学问题。
因此,建议各位看官如果是按照我的环境进行配置的话,请通过以下方式进行安装。
2.1 CPU版本
首先添加清华镜像源提高速度。 主要是前三行和最后一行 其余行可自行考虑是否添加
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/peterjc123/
conda config --set show_channel_urls yes
添加完成后,可运行以下代码进行安装。
conda install pytorch torchvision torchaudio cudatoolkit=11.0
2.2 GPU版本
在按照pytorch_geometric环境配置一完成cuda的安装后(参考:深度学习GPU环境CUDA详细安装过程(简单快速有效) - 知乎 (zhihu.com)),按照上述操作添加清华镜像源后,仅将最后的安装代码换为下一行即可。
# CUDA 11.0
pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
2.3 环境测试
在编辑器如Pycharm或Jupyter notebook中运行如下代码:
import torch
print(torch.__version__)
print('gpu:', torch.cuda.is_available())
结果如下:
1.7.1+cu110
gpu: True
上面是版本号,下面是书否安装cuda版本,如果是即为True 反之则为False