Pytorch 安装
官网如下:
进入官网后选择 Install,在下面表格中按照你的配置进行选择:
其中 Package 部分选择安装的途径,这里主要介绍 Pip 和 Conda 两种途径。
通过 Pip 安装
Pip 在通过 python 官网下载 python 并安装时可以选择同时安装 pip,不需要额外安装 Anaconda,比较方便。
- 根据你的系统、CUDA 版本等配置在表中选择,最后复制表格最下面生成的指令。
- Win+R 唤出 cmd 命令行窗口,将指令粘贴并运行,然后会生成下载计划并在最后附上 [y/n],输入 y 并等待下载完成即可。完整的环境大概有 2.9G 且从官网下载,如果没有挂梯子需要等待较长时间。也可以配置镜像源,方法按照接下来的步骤。
- 对于 Windows 用户,在 C:\Users\xx\pip 目录下(没有 pip 目录就新建)创建一个 pip.ini 文件,并将下面代码块中内容复制进去:
- 对于 Linux 用户,同样在~/.pip/pip.conf 进行配置。如果没有.pip 目录就新建,然后将下面代码块中内容复制进去:
[global]
index-url = http://pypi.douban.com/simple
extra-index-url = https://pypi.mirrors.ustc.edu.cn/simple/
https://mirrors.aliyun.com/pypi/simple/
https://pypi.tuna.tsinghua.edu.cn/simple/
https://pypi.org/simple/
trusted-host = pypi.mirrors.ustc.edu.cn
mirrors.aliyun.com
pypi.tuna.tsinghua.edu.cn
pypi.org
pypi.douban.com
通过 Conda 安装
因为 Conda 可以配置并切换虚拟环境,较为方便的下载各种库,这里更推荐使用 Conda 配环境。
- 以管理员身份打开 Anaconda Prompt。这是一个操作 conda 的命令行窗口。不给管理员权限最后可能下载完成后无法安装。
- 按照你的配置在官网选择,记得把 Package 改为 conda,复制命令到 Anaconda Prompt 运行。
- 接下来 conda 会开始 solving environment,这个过程需要较长时间,并且 conda 会自动尝试多次,笔者在重配环境时平均每次 solving environment 需要 15min 左右,这一过程结束后才开始获取并生成下载计划,最后显示 [y/n] 询问是否开始下载。输入 y 回车开始下载。过程中请保证网络稳定!!!否则会下载失败终止进程,需要重新输入命令开始下载并等待再次 solving environment,相当折磨。
- 如果需要加快下载速度,可以在 Anaconda 中添加新的 channel 来换源加快下载速度,方法主要有两种:一是通过 Anaconda Navigaiton,在左边选择 Environments,在上方选择 Channels,Add,最后记得 Update Channels 即可(某些 channel 通过这种方式添加好像会显示 invalid, 但是通过下一种方法却可以成功添加,原理未知);或者在 Anaconda Prompt 执行以下指令:
bash
conda config --set show_channel_urls yes
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/conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
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 --show channels
Tips:关于如何查看自己设备信息
Windows
同时按下 Win+R, 运行 cmd, 输入 dxdiag
并回车。系统、显卡、声卡以及其他设备信息都会显示。
cuda 版本查看