📦 安装配置
在开始学习PyTorch之前,我们需要先把环境搭建好。别担心,这个过程很简单!
📌 当前最新版本
PyTorch 稳定版:2.7.0 (2025年)
参考:PyTorch官方安装指南
🔧 环境要求
Python 版本要求
| 操作系统 | Python 版本要求 |
|---|---|
| Windows | Python 3.9 - 3.13 |
| Linux | Python 3.10 - 3.14 |
| macOS | Python 3.10 - 3.14 |
⚠️ 重要提示
最新稳定版 PyTorch 2.7.0 要求 Python 3.10 或更高版本! 如果你使用的是 Python 3.8 或 3.9,请先升级 Python。
操作系统要求
- Windows: Windows 7 及以上(推荐 Windows 10+)
- Linux: glibc >= 2.28(Ubuntu 20.04+、CentOS 8+、Debian 10+)
- macOS: macOS 10.15 (Catalina) 及以上
🎯 安装方式
方式一:使用pip安装(推荐新手)
打开命令行(Windows用户打开CMD或PowerShell),执行:
# CPU版本(如果你没有NVIDIA显卡)
pip3 install torch torchvision torchaudio
# 或者指定清华镜像源(国内更快)
pip3 install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple
方式二:使用conda安装
如果你使用Anaconda或Miniconda:
# 创建新环境(推荐使用 Python 3.10+)
conda create -n pytorch python=3.11
# 激活环境
conda activate pytorch
# 安装PyTorch CPU版本
conda install pytorch torchvision torchaudio cpuonly -c pytorch
方式三:GPU版本 - NVIDIA CUDA(有NVIDIA显卡的用户)
⚠️ 注意
GPU版本需要先安装NVIDIA驱动和CUDA。如果你是新手,建议先用CPU版本学习。
# CUDA 11.8版本
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
# CUDA 12.6版本(推荐)
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
# CUDA 12.8版本(最新)
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128
💡 如何选择CUDA版本?
通常选择最新的CUDA版本性能更好。运行 nvidia-smi 可以查看你的显卡支持的最高CUDA版本。
方式四:GPU版本 - AMD ROCm(有AMD显卡的用户)
如果你使用的是AMD显卡(仅限Linux):
# ROCm 6.3版本
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm6.3
ℹ️ ROCm说明
ROCm是AMD的GPU计算平台,类似于NVIDIA的CUDA。PyTorch的ROCm版本在Python API层面与CUDA版本完全兼容,torch.cuda.is_available() 同样可以用于检测ROCm设备。
✅ 验证安装
安装完成后,让我们验证一下是否成功。
基础验证
import torch
# 打印PyTorch版本
print(f"PyTorch版本: {torch.__version__}")
# 创建一个随机张量(官方推荐的验证方式)
x = torch.rand(5, 3)
print(x)
预期输出(数值会不同):
PyTorch版本: 2.7.0
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/ROCm)
如果你安装了GPU版本,运行以下代码验证:
import torch
# 检查CUDA/ROCm是否可用
print(f"CUDA可用: {torch.cuda.is_available()}")
# 如果可用,查看更多信息
if torch.cuda.is_available():
print(f"CUDA版本: {torch.version.cuda}")
print(f"GPU设备数量: {torch.cuda.device_count()}")
print(f"当前GPU: {torch.cuda.get_device_name(0)}")
# 在GPU上创建张量测试
x = torch.rand(3, 3).cuda()
print(f"GPU张量: {x}")
💡 ROCm用户
PyTorch的ROCm版本使用相同的API,torch.cuda.is_available() 同样适用于检测AMD GPU。
如果没有报错,并且能看到输出,恭喜你安装成功!🎉
🛠️ 推荐的开发环境
1. Jupyter Notebook(强烈推荐新手)
pip install jupyter
# 启动Jupyter
jupyter notebook
优点:
- 可以一段一段运行代码
- 即时看到输出结果
- 方便调试和学习
2. VS Code + Python插件
- 安装VS Code
- 安装Python插件
- 安装Jupyter插件(可以在VS Code中运行.ipynb文件)
3. PyCharm
专业的Python IDE,功能强大,但对新手可能有点复杂。
📁 项目结构建议
学习时,建议按照以下结构组织你的代码:
pytorch-learning/
├── 01_basics/
│ ├── tensor_demo.py
│ └── autograd_demo.py
├── 02_neural_network/
│ ├── simple_nn.py
│ └── training.py
├── 03_projects/
│ ├── mnist/
│ └── image_classifier/
└── requirements.txt
❓ 常见问题
Q1: 安装时报错 "No matching distribution found"
原因:Python版本不兼容。PyTorch 2.7.0 要求 Python 3.10+
解决方案:
# 查看当前Python版本
python --version
# 如果版本过低,升级Python或创建新的虚拟环境
conda create -n pytorch python=3.11
conda activate pytorch
Q2: 导入torch时报错 "ModuleNotFoundError"
原因:PyTorch未正确安装,或安装在了不同的Python环境
解决方案:
# Windows PowerShell
pip list | Select-String torch
# Linux/macOS
pip list | grep torch
# 如果没有找到,重新安装
pip3 install torch torchvision torchaudio
Q3: CUDA版本不匹配 / GPU不可用
解决方案:
# 1. 查看显卡驱动支持的CUDA版本
nvidia-smi
# 2. 查看已安装的PyTorch CUDA版本
python -c "import torch; print(torch.version.cuda)"
# 3. 如果版本不匹配,重新安装对应版本
# 例如显卡支持CUDA 12.x,安装CUDA 12.6版本
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
Q4: macOS上安装失败
解决方案:macOS不支持CUDA,只能使用CPU版本或MPS(Apple Silicon)
# macOS安装命令
pip3 install torch torchvision torchaudio
# Apple Silicon (M1/M2/M3) 可以使用MPS加速
python -c "import torch; print(torch.backends.mps.is_available())"
Q5: 安装速度太慢
解决方案:使用国内镜像源
# 清华镜像
pip3 install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple
# 阿里云镜像
pip3 install torch torchvision torchaudio -i https://mirrors.aliyun.com/pypi/simple
📚 官方资源
- 🔗 PyTorch官方安装指南 - 最新安装命令生成器
- 🔗 PyTorch官方文档 - API文档和教程
- 🔗 PyTorch论坛 - 社区问答
下一步
安装完成后,让我们开始学习张量基础!