目录
一、环境介绍
PyTorch 系统要求
- 操作系统:Windows 10/11 64位
- Python 版本:3.9 - 3.13(不支持 Python 2.x)
- 官方资源 :
推荐配置参考
| 组件 | 推荐配置 | 说明 |
|---|---|---|
| CPU | Intel i7 或同级以上 | 建议 8 核心以上 |
| 显卡 | NVIDIA RTX 2060 及以上 | 显存建议 8GB+ |
| 操作系统 | Windows 10/11 64位 | 确保系统更新到最新版本 |
| Anaconda | 最新版本 | 便于环境管理 |
| CUDA Toolkit | 11.8 / 12.1 / 12.6 | 根据显卡驱动选择 |
| cuDNN | 8.x 系列 | 与 CUDA 版本匹配 |
| Python | 3.10 / 3.11 | 稳定性较好 |
| PyTorch | 2.2.2 / 2.8.0 | 根据需求选择稳定版或最新版 |
二、安装前准备
1. 检查显卡驱动
在安装 CUDA 之前,请确保显卡驱动已更新到最新版本:
bash
# Windows 命令行查看显卡信息
nvidia-smi
2. 确定 CUDA 版本
bash
# 查看已安装的 CUDA 版本
nvcc -V
3. 版本匹配参考
| PyTorch 版本 | CUDA 版本 | Python 版本 | 适用场景 |
|---|---|---|---|
| 2.8.0 | 12.6 | 3.11+ | 最新特性,新显卡 |
| 2.2.2 | 12.1 | 3.10-3.11 | 稳定版本,兼容性好 |
| 2.2.2 | 11.8 | 3.8-3.11 | 旧显卡,广泛兼容 |
| 2.2.2 | CPU | 3.8-3.11 | 无独立显卡 |
三、安装教程
方案一:使用 Conda 安装(推荐)
bash
# 创建新环境
conda create -n pytorch python=3.11
conda activate pytorch
# CUDA 11.8 版本
conda install pytorch==2.2.2 torchvision==0.17.2 torchaudio==2.2.2 pytorch-cuda=11.8 -c pytorch -c nvidia
# CUDA 12.1 版本
conda install pytorch==2.2.2 torchvision==0.17.2 torchaudio==2.2.2 pytorch-cuda=12.1 -c pytorch -c nvidia
# CUDA 12.6 版本
conda install pytorch torchvision torchaudio pytorch-cuda=12.6 -c pytorch -c nvidia
方案二:使用 Pip 安装
bash
# CUDA 11.8
pip install torch==2.2.2 torchvision==0.17.2 torchaudio==2.2.2 --index-url https://download.pytorch.org/whl/cu118
# CUDA 12.1
pip install torch==2.2.2 torchvision==0.17.2 torchaudio==2.2.2 --index-url https://download.pytorch.org/whl/cu121
# CUDA 12.6
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
# CPU 版本(无显卡)
pip install torch==2.2.2 torchvision==0.17.2 torchaudio==2.2.2 --index-url https://download.pytorch.org/whl/cpu
方案三:使用国内镜像源加速
bash
# 清华镜像源
pip install torch==2.2.2 torchvision==0.17.2 torchaudio==2.2.2 -i https://pypi.tuna.tsinghua.edu.cn/simple
# 安装常用依赖
pip install facenet-pytorch -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install numpy==1.26.4 -i https://pypi.tuna.tsinghua.edu.cn/simple
# 安装 dlib(需要 conda)
conda install -c conda-forge dlib
# 安装 face-recognition
pip install face-recognition -i https://pypi.tuna.tsinghua.edu.cn/simple
Conda 常用命令
bash
# 搜索可用版本
conda search pytorch -c pytorch
conda search pytorch-cuda -c pytorch -c nvidia
# 查看通道配置
conda config --show
# 添加通道
conda config --add channels 通道地址
# 删除环境
conda remove --name 环境名称 --all
conda remove --prefix 路径 --all
# 卸载包
pip uninstall torch torchvision torchaudio
# 查看已安装包
conda list
四、环境验证
1. 快速验证
bash
# 命令行验证
python -c "import torch; print(torch.__version__)"
python -c "import torch; print(torch.cuda.is_available())"
2. Python 脚本验证
python
import torch
print(f"PyTorch 版本:{torch.__version__}")
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)}")
3. CPU 测试
python
import torch
# 创建随机张量
x = torch.rand(5, 3)
print(x)
print(f"设备:{x.device}")
4. GPU 完整测试
python
import torch
import torch.nn as nn
# 检查 CUDA 是否可用
if torch.cuda.is_available():
device = torch.device("cuda")
print("✓ 运行在 GPU 上")
else:
device = torch.device("cpu")
print("✓ 运行在 CPU 上")
# 创建模型并移动到设备
model = nn.Linear(10, 5).to(device)
# 创建输入数据
x = torch.randn(3, 10).to(device)
# 执行前向传播
output = model(x)
print(f"输出设备:{output.device}")
print(f"输出形状:{output.shape}")
print(f"输出值:\n{output}")
五、常见问题与解决方案
问题 1:torch.accelerator 属性错误
错误信息:
AttributeError: module 'torch' has no attribute 'accelerator'
原因 :torch.accelerator 属性在低版本 PyTorch 中未定义。
解决方案:
python
# ❌ 错误写法
device = torch.accelerator.current_accelerator().type if torch.accelerator.is_available() else "cpu"
# ✅ 正确写法 1
device = "cuda" if torch.cuda.is_available() else "cpu"
# ✅ 正确写法 2
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device) # 输出:cuda:0 或 cpu
问题 2:CUDA 版本不匹配
症状 :安装后 torch.cuda.is_available() 返回 False
解决方案:
-
检查显卡驱动版本是否支持所选 CUDA 版本
-
重新安装匹配的 CUDA Toolkit
-
使用以下命令确认:
bashnvidia-smi # 查看驱动支持的最高 CUDA 版本
问题 3:安装速度慢
解决方案:
bash
# 使用清华镜像
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple 包名
# 使用阿里镜像
pip install -i https://mirrors.aliyun.com/pypi/simple/ 包名
# 配置永久镜像源
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
问题 4:dlib 安装失败
解决方案:
bash
# 先安装 CMake
conda install -c conda-forge cmake
# 再安装 dlib
conda install -c conda-forge dlib
# 最后安装 face-recognition
pip install face-recognition
问题 5:环境冲突
解决方案:
bash
# 完全卸载
pip uninstall torch torchvision torchaudio
# 清理缓存
pip cache purge
# 重新创建环境
conda remove --name pytorch --all
conda create -n pytorch python=3.11
conda activate pytorch
六、使用教程与资源
官方教程
中文资源
视频教程
社区资源
七、代码示例
官方代码仓库
基础示例
python
# 1. 张量操作
import torch
x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5, 6])
print(x + y) # 张量加法
# 2. 自动求导
x = torch.tensor([2.0], requires_grad=True)
y = x ** 2
y.backward()
print(x.grad) # 输出:4.0
# 3. 神经网络
import torch.nn as nn
class Net(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(10, 5)
self.fc2 = nn.Linear(5, 2)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
model = Net()
print(model)
附录:版本选择建议
| 使用场景 | 推荐配置 |
|---|---|
| 学习入门 | PyTorch 2.2.2 + CUDA 11.8 + Python 3.10 |
| 生产环境 | PyTorch 2.2.2 + CUDA 12.1 + Python 3.11 |
| 最新特性 | PyTorch 2.8.0 + CUDA 12.6 + Python 3.11+ |
| 无独立显卡 | PyTorch CPU 版本 + Python 3.11 |
| 旧显卡支持 | PyTorch 2.2.2 + CUDA 11.2 + Python 3.9 |
提示:建议定期关注 PyTorch 官方更新,及时升级以获得更好的性能和安全性。如有问题,优先查阅官方文档或在社区寻求帮助。