安装 Pytorch GPU 版的正确方法

本文介绍安装 Pytorch GPU 版本的正确方法

1 前言

目前,网上已经有很多资料介绍如何安装 Pytorch GPU 版本。然而,这些文章中往往包含了一些不必要的步骤。其实安装 Pytorch GPU 版本非常简单,并不需要手动安装 CUDA 和 CuDNN。

2 准备工作

  1. 安装前,首先确保你的显卡是 NVIDIA 的,可以通过任务管理器查看。(AMD 显卡不支持 Windows,步骤类似)
  2. 下载 Geforce Experience 更新显卡驱动,需要注册一个帐号。不建议使用鲁大师等软件更新驱动,这类软件上的驱动通常不是最新的,可能造成问题。
  3. 安装 Anaconda

3 安装 Pytorch

安装前,国内用户最好将下载源更改为清华镜像,否则下载速度将会很慢。

打开 Anaconda Prompt,输入

1
conda config --set show_channel_urls yes

然后打开 C:\Users\你的用户名\ 目录下的 .condarc 文件,把原有内容替换为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
channels:
- defaults
show_channel_urls: true
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

然后输入以下语句

1
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch

4 验证是否安装成功

运行如下代码

1
2
import torch
print(torch.cuda.is_available())

如果输出 True,就安装成功了。

5 参考资料

  1. https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/
  2. https://pytorch.org/get-started/locally/
原创技术分享,您的支持将鼓励我继续创作