一、环境介绍
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 之前,请确保显卡驱动已更新到最新版本:
# windows 命令行查看显卡信息 nvidia-smi
2. 确定 cuda 版本
# 查看已安装的 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 安装(推荐)
# 创建新环境 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 安装
# 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
方案三:使用国内镜像源加速
# 清华镜像源 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 常用命令
# 搜索可用版本 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. 快速验证
# 命令行验证 python -c "import torch; print(torch.__version__)" python -c "import torch; print(torch.cuda.is_available())"
2. 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 测试
import torch
# 创建随机张量
x = torch.rand(5, 3)
print(x)
print(f"设备:{x.device}")
4. gpu 完整测试
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 中未定义。
解决方案:
# ❌ 错误写法
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
- 使用以下命令确认:
nvidia-smi # 查看驱动支持的最高 cuda 版本
问题 3:安装速度慢
解决方案:
# 使用清华镜像 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 安装失败
解决方案:
# 先安装 cmake conda install -c conda-forge cmake # 再安装 dlib conda install -c conda-forge dlib # 最后安装 face-recognition pip install face-recognition
问题 5:环境冲突
解决方案:
# 完全卸载 pip uninstall torch torchvision torchaudio # 清理缓存 pip cache purge # 重新创建环境 conda remove --name pytorch --all conda create -n pytorch python=3.11 conda activate pytorch
六、代码示例
官方代码仓库
基础示例
# 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 官方更新,及时升级以获得更好的性能和安全性。如有问题,优先查阅官方文档或在社区寻求帮助。
到此这篇关于windows下pytorch环境搭建指南的文章就介绍到这了,更多相关pytorch 环境搭建内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论