原文:linux 服务器使用 conda 安装 pytorch

1. 安装 anaconda

1)下载 anaconda

  • 命令行下载

    1
    wget https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh

2)安装 anaconda

1
bash Anaconda3-4.2.0-Linux-x86_64.sh

3)切换镜像源

1
2
3
4
5
6
7
# 清华镜像源
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.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes

删除镜像源:

1
conda config --remove-key channels

4)检查是否安装成功

1
conda
在这里插入图片描述

2. 安装 pytorch

1)创建虚拟环境

此步可省略,直接装在base环境上

1
conda create -n torch python=3.7 numpy matplotlib pandas anaconda
在这里插入图片描述

2)激活环境

1
source activate torch

3)查看 cuda环境

1
nvcc -V

在这里插入图片描述

注意事先需要安装好 cuda 和 cudnn 驱动,这里可以看到 cuda 为 10.0 版本

4)安装 pytorch

不同的 pytorch 和 cuda 版本会对应不同的一行 conda install 指令,pytorch 官网会给出不同 pytorch 和cuda 搭配安装的不同指令。具体指令可以在 pytorch官网找到:

在这里插入图片描述

由于墙外下载下载较慢,甚至卡死,因此我们使用国内镜像源下载安装,在之前,我们添加了清华的镜像源,所以我们可以去掉-c pytorch,因此,pytorch 安装命令为:

1
2
# 不用加 -c 为用国内源
conda install pytorch torchvision cudatoolkit=10.0

这里 cudatoolkit 改为 10.0

3. cuda 测试

1
2
3
4
5
6
7
8
# cuda test
import torch
x = torch.Tensor([1.0])
xx = x.cuda()
print(xx)
# cuDNN test
from torch.backends import cudnn
print(cudnn.is_acceptable(xx))
在这里插入图片描述