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 torchDownloading torch-2.1.0-cp39-cp39-win_amd64.whl.metadata (24 kB)
Collecting torchvisionDownloading torchvision-0.16.0-cp39-cp39-win_amd64.whl.metadata (6.6 kB)
Collecting torchaudioDownloading 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深度学习与企业级项目实战》,获出版社和作者授权发布。