pytorch安装

一、准备

建议使用conda安装,便于包的管理以及与其他环境相隔离

conda安装请移步
conda使用点这里

二、安装

1、conda安装pytorch

# conda安装pytorch,linux和windows平台安装命令一样
# 创建pytorch环境
conda create --name pytorch python=3.8
# 进入pytorch环境
conda activate pytorch
# 安装gpu pytorch环境
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
# 如果没有gpu可安装cpu版,安装命令如下
# conda install pytorch torchvision torchaudio cpuonly -c pytorch

2、pip安装pytorch

# linux环境安装
# 安装gpu版本
pip3 install torch==1.10.2+cu113 torchvision==0.11.3+cu113 torchaudio==0.10.2+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
# 安装cpu版本
pip3 install torch==1.10.2+cpu torchvision==0.11.3+cpu torchaudio==0.10.2+cpu -f https://download.pytorch.org/whl/cpu/torch_stable.html
# windows环境安装
# 安装gpu版本
pip3 install torch==1.10.2+cu113 torchvision==0.11.3+cu113 torchaudio===0.10.2+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
# 安装cpu版本
pip3 install torch torchvision torchaudio

三、验证

在控制台输出一下命令

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

验证cuda驱动是否可用

import torch
torch.cuda.is_available()

你可能感兴趣的:(PyTorch,pytorch,深度学习,人工智能)