我的配置
Ubuntu 16.04
anaconda3(Python3.7)
准备工作
参考这篇文章:Pytorch安装教程
需要做的有三点:
- 添加Anaconda的TUNA镜像(直接安装就会被墙)
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- 设置搜索时显示通道地址(这一项应该不是必须的)
conda config --set show_channel_urls yes
- 执行过conda config指令后,就会生成~/.condarc文件,修改这个文件,删除其中的default行
sudo gedit .condarc
修改后的.condarc文件长这样:
这一步很重要,否则PyTorch安装过程中会报An HTTP error occurred when trying to retrieve this URL的错
安装
PyTorch安装指令经常更改,最好进入官网查看
官网会根据你选择的环境给出相应的安装指令,复制粘贴就行了
我装的时候是这样的:
官网给出的指令
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
测试
- 进入Python,运行下面一段程序
import torch
x = torch.rand(5, 3)
print(x)
输出的结果类似这样,表明PyTorch已经正确安装
tensor([[0.3380, 0.3845, 0.3217],
[0.8337, 0.9050, 0.2650],
[0.2979, 0.7141, 0.9069],
[0.1449, 0.1132, 0.1375],
[0.4675, 0.3947, 0.1426]])
- 运行下面的程序:
import torch
torch.cuda.is_available()
检查PyTorch能否访问并启用GPU驱动和CUDA
(返回true表示可)