跳转至

PyTorch 快速开始

1. 本地安装

Start Locally 查询对应的安装命令。系统推荐使用 Anaconda 。由于我系统中安装了一些内容,所以选择使用 venv pip 的安装方式。

CUDA 下载页面

CUDA GPU 支持列表

安装 CUDA 前需要安装对应显卡驱动和 Visual Studio 版本,具体信息可以在 CUDA 下载页面中查找到。

CUDA Samples 路径:C:\ProgramData\NVIDIA Corporation\CUDA Samples\v11.1

# Anaconda No Cuda
conda install pytorch torchvision torchaudio cpuonly -c pytorch

# Anaconda With Cuda
conda install pytorch torchvision torchaudio cudatoolkit=11.1 -c pytorch -c nvidia

# pip No Cuda
pip3 install torch==1.8.1+cpu torchvision==0.9.1+cpu torchaudio===0.8.1 -f https://download.pytorch.org/whl/torch_stable.html

# pip With Cuda
pip3 install torch==1.8.1+cu111 torchvision==0.9.1+cu111 torchaudio===0.8.1 -f https://download.pytorch.org/whl/torch_stable.html

2. 验证

为了确保 PyTorch 安装正确,我们可以通过运行样本 PyTorch 代码来验证安装。在这里,我们将构建一个随机初始化的拉伸器。

python
# 然后输入如下代码:
import torch
x = torch.rand(5, 3)
print(x)

应该有类似如下输出:

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]])

此外,要检查您的 GPU 驱动程序和 CUDA 是否由 PyTorch 启用并访问,请运行以下命令以返回 CUDA 驱动程序是否启用:

import torch
torch.cuda.is_available()

创建日期: 2021-05-01 01:00:00
最后更新: 2022-07-30 02:00:00