科技 > 操作系统 > Windows

Windows下PyTorch环境搭建指南

1人参与 2026-04-08 Windows

一、环境介绍

pytorch 系统要求

推荐配置参考

组件推荐配置说明
cpuintel i7 或同级以上建议 8 核心以上
显卡nvidia rtx 2060 及以上显存建议 8gb+
操作系统windows 10/11 64位确保系统更新到最新版本
anaconda最新版本便于环境管理
cuda toolkit11.8 / 12.1 / 12.6根据显卡驱动选择
cudnn8.x 系列与 cuda 版本匹配
python3.10 / 3.11稳定性较好
pytorch2.2.2 / 2.8.0根据需求选择稳定版或最新版

二、安装前准备

1. 检查显卡驱动

在安装 cuda 之前,请确保显卡驱动已更新到最新版本:

# windows 命令行查看显卡信息
nvidia-smi

2. 确定 cuda 版本

# 查看已安装的 cuda 版本
nvcc -v

3. 版本匹配参考

pytorch 版本cuda 版本python 版本适用场景
2.8.012.63.11+最新特性,新显卡
2.2.212.13.10-3.11稳定版本,兼容性好
2.2.211.83.8-3.11旧显卡,广泛兼容
2.2.2cpu3.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

解决方案

  1. 检查显卡驱动版本是否支持所选 cuda 版本
  2. 重新安装匹配的 cuda toolkit
  3. 使用以下命令确认:
    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 环境搭建内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

您想发表意见!!点此发布评论

推荐阅读

MoneyPrinterTurbo 新手小白入门指南

04-08

如何使用MoneyPrinterTurbo在Windows上自动生成抖音短视频(附避坑指南)

04-08

内存占用飙升! Win11新版Copilot改用内置Edge浏览器

04-07

Windows系统上安装Jenkins的完整流程及关键注意事项

04-08

MessageUtils.message("user.jcaptcha.expire")问题及解决

04-08

触控笔可一键唤起Copilot! Win11 Canary预览版29560.1000/28020.1803发布

04-07

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论