在Windows环境下安装CPU版的PyTorch

PytTorch是基于Python开发的,首先需要安装Python,Python的安装很简单,这里不再赘述。而 Windows用户能直接通过conda、pip和源码编译三种方式来安装PyTorch。

打开PyTorch官网(PyTorch),在主页中根据自己的计算机选择Linux、Mac或Windows系统,如图2-18所示,系统将给出对应的安装命令语句,比如这里为pip3 install torch torchvision torchaudio。

图2-18

通过安装命令安装PyTorch 2.1.0,结果如下:

PS C:\Users\xiayu> pip3 install torch torchvision torchaudio
Collecting torch
  Downloading torch-2.1.0-cp39-cp39-win_amd64.whl.metadata (24 kB)
Collecting torchvision
  Downloading torchvision-0.16.0-cp39-cp39-win_amd64.whl.metadata (6.6 kB)
Collecting torchaudio
  Downloading torchaudio-2.1.0-cp39-cp39-win_amd64.whl.metadata (5.7 kB)
Collecting filelock (from torch)
  Downloading filelock-3.12.4-py3-none-any.whl.metadata (2.8 kB)
Collecting typing-extensions (from torch)
  Downloading typing_extensions-4.8.0-py3-none-any.whl.metadata (3.0 kB)
Collecting sympy (from torch)
  Downloading sympy-1.12-py3-none-any.whl (5.7 MB)
   ━━━━━━━━━━━━━━━━━━ 5.7/5.7 MB 14.7 kB/s eta 0:00:00
Collecting networkx (from torch)
  Downloading networkx-3.2-py3-none-any.whl.metadata (5.2 kB)
Collecting jinja2 (from torch)
  Downloading Jinja2-3.1.2-py3-none-any.whl (133 kB)
   ━━━━━━━━━━━━━━━━━━ 133.1/133.1 kB 12.8 kB/s eta 0:00:00
Collecting fsspec (from torch)
  Downloading fsspec-2023.10.0-py3-none-any.whl.metadata (6.8 kB)
Requirement already satisfied: numpy in 
c:\users\xiayu\appdata\local\programs\python\python39\lib\site-packages (from torchvision) (1.26.1)
Collecting requests (from torchvision)
  Downloading requests-2.31.0-py3-none-any.whl.metadata (4.6 kB)
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in 
c:\users\xiayu\appdata\local\programs\python\python39\lib\site-packages (from torchvision) (10.1.0)
Collecting MarkupSafe>=2.0 (from jinja2->torch)
  Downloading MarkupSafe-2.1.3-cp39-cp39-win_amd64.whl.metadata (3.1 kB)
Collecting charset-normalizer<4,>=2 (from requests->torchvision)
  Downloading charset_normalizer-3.3.1-cp39-cp39-win_amd64.whl.metadata (33 kB)
Collecting idna<4,>=2.5 (from requests->torchvision)
  Downloading idna-3.4-py3-none-any.whl (61 kB)
   ━━━━━━━━━━━━━━━━━━ 61.5/61.5 kB 32.2 kB/s eta 0:00:00
Collecting urllib3<3,>=1.21.1 (from requests->torchvision)
  Downloading urllib3-2.0.7-py3-none-any.whl.metadata (6.6 kB)
Collecting certifi>=2017.4.17 (from requests->torchvision)
  Downloading certifi-2023.7.22-py3-none-any.whl.metadata (2.2 kB)
Collecting mpmath>=0.19 (from sympy->torch)
  Downloading mpmath-1.3.0-py3-none-any.whl (536 kB)
   ━━━━━━━━━━━━━━━━━━ 536.2/536.2 kB 17.7 kB/s eta 0:00:00
Downloading torch-2.1.0-cp39-cp39-win_amd64.whl (192.2 MB)
   ━━━━━━━━━━━━━━━━━━ 192.2/192.2 MB 96.2 kB/s eta 0:00:00
Downloading torchvision-0.16.0-cp39-cp39-win_amd64.whl (1.3 MB)
   ━━━━━━━━━━━━━━━━━━ 1.3/1.3 MB 78.0 kB/s eta 0:00:00
Downloading torchaudio-2.1.0-cp39-cp39-win_amd64.whl (2.3 MB)
   ━━━━━━━━━━━━━━━━━━ 2.3/2.3 MB 78.5 kB/s eta 0:00:00
Downloading filelock-3.12.4-py3-none-any.whl (11 kB)
Downloading fsspec-2023.10.0-py3-none-any.whl (166 kB)
   ━━━━━━━━━━━━━━━━━━ 166.4/166.4 kB 121.9 kB/s eta 0:00:00
Downloading networkx-3.2-py3-none-any.whl (1.6 MB)
   ━━━━━━━━━━━━━━━━━━ 1.6/1.6 MB 81.6 kB/s eta 0:00:00
Downloading requests-2.31.0-py3-none-any.whl (62 kB)
   ━━━━━━━━━━━━━━━━━━ 62.6/62.6 kB 119.8 kB/s eta 0:00:00
Downloading typing_extensions-4.8.0-py3-none-any.whl (31 kB)
Downloading certifi-2023.7.22-py3-none-any.whl (158 kB)
   ━━━━━━━━━━━━━━━━━━ 158.3/158.3 kB 103.1 kB/s eta 0:00:00
Downloading charset_normalizer-3.3.1-cp39-cp39-win_amd64.whl (98 kB)
   ━━━━━━━━━━━━━━━━━━ 98.7/98.7 kB 111.1 kB/s eta 0:00:00
Downloading MarkupSafe-2.1.3-cp39-cp39-win_amd64.whl (17 kB)
Downloading urllib3-2.0.7-py3-none-any.whl (124 kB)
   ━━━━━━━━━━━━━━━━━━ 124.2/124.2 kB 165.7 kB/s eta 0:00:00
Installing collected packages: mpmath, urllib3, typing-extensions, sympy, networkx, MarkupSafe, idna, fsspec, filelock, charset-normalizer, certifi, requests, jinja2, torch, torchvision, torchaudio
Successfully installed MarkupSafe-2.1.3 certifi-2023.7.22 charset-normalizer-3.3.1 filelock-3.12.4 fsspec-2023.10.0 idna-3.4 jinja2-3.1.2 mpmath-1.3.0 networkx-3.2 requests-2.31.0 sympy-1.12 torch-2.1.0 torchaudio-2.1.0 torchvision-0.16.0 typing-extensions-4.8.0 urllib3-2.0.7
WARNING: There was an error checking the latest version of pip.
PS C:\Users\xiayu>

验证PyTorch是否安装成功,执行如下命令,注意命令中的双下画线:

print(torch.__version__)
print(torch.version.cuda)
print(torch.cuda.is_available())

命令执行结果如下:

PS C:\Users\xiayu> python
Python 3.9.10 (tags/v3.9.10:f2f3f53, Jan 17 2022, 15:14:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> print(torch.__version__) 
2.1.0+cpu
>>> print(torch.version.cuda)
None
>>> print(torch.cuda.is_available())
False
>>>

如果没有报错,则说明PyTorch安装成功。

《PyTorch深度学习与企业级项目实战(人工智能技术丛书)》(宋立桓,宋立林)【摘要 书评 试读】- 京东图书 (jd.com)

本文节选自《PyTorch深度学习与企业级项目实战》,获出版社和作者授权发布。

相关推荐
懒大王爱吃狼21 分钟前
Python教程:python枚举类定义和使用
开发语言·前端·javascript·python·python基础·python编程·python书籍
深度学习实战训练营26 分钟前
基于CNN-RNN的影像报告生成
人工智能·深度学习
秃头佛爷1 小时前
Python学习大纲总结及注意事项
开发语言·python·学习
昨日之日20062 小时前
Moonshine - 新型开源ASR(语音识别)模型,体积小,速度快,比OpenAI Whisper快五倍 本地一键整合包下载
人工智能·whisper·语音识别
浮生如梦_2 小时前
Halcon基于laws纹理特征的SVM分类
图像处理·人工智能·算法·支持向量机·计算机视觉·分类·视觉检测
深度学习lover2 小时前
<项目代码>YOLOv8 苹果腐烂识别<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·苹果腐烂识别
热爱跑步的恒川3 小时前
【论文复现】基于图卷积网络的轻量化推荐模型
网络·人工智能·开源·aigc·ai编程
API快乐传递者3 小时前
淘宝反爬虫机制的主要手段有哪些?
爬虫·python
阡之尘埃5 小时前
Python数据分析案例61——信贷风控评分卡模型(A卡)(scorecardpy 全面解析)
人工智能·python·机器学习·数据分析·智能风控·信贷风控
孙同学要努力7 小时前
全连接神经网络案例——手写数字识别
人工智能·深度学习·神经网络