PyTorch 官方提供了几种安装方法,可以通过 pip 或 conda 进行安装。
CPU 版本安装
使用 pip 安装 pytorch:
# 最新稳定版本
pip install torch torchvision torchaudio
# 指定版本
pip install torch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0
# 仅 CPU 版本(更小的安装包)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
GPU版本安装
传统的安装方法:
1. 安装NVIDIA驱动
2. 安装CUDA Toolkit(完整开发包,1-3GB)
3. 安装cuDNN(深度学习加速库)
4. 安装PyTorch
而我采用的安装方法:
1. 安装NVIDIA驱动(已有)
2. 直接安装PyTorch(包含所需CUDA组件) ✓
比如我采用的命令:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118