PyTorch是机器学习和深度学习领域的重要工具,以下是安装指南:
安装方法 🧰
通过pip安装
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
✅ 适用于大多数Python环境,支持CUDA加速(需先安装NVIDIA驱动)。
从源码编译
🛠️ 需要CMake和构建工具,适合定制化需求:git clone https://github.com/pytorch/pytorch.git cd pytorch python setup.py build develop
使用Conda
🐍 安装Anaconda后运行:conda install pytorch torchvision torchaudio pytorch-cuda=118 -c pytorch
验证安装 ✅
import torch
print(torch.__version__)
print(torch.cuda.is_available()) # 检查CUDA是否可用
常见问题 ❓
- 依赖冲突:确保Python版本与PyTorch兼容(推荐3.8-3.11)。
- 网络问题:使用
--index-url
指定镜像源,如清华源:--index-url https://pypi.tuna.tsinghua.edu.cn/simple
延伸学习 📚
安装完成后,可进一步学习: