1.使用自带工具安装

依次打开菜单:File->Setting

点击+号,搜索torch

选择1.13.0版本安装

安装错误:

具体包含很长一段错误记录,如下:

Collecting torch==1.13.0Downloading https://files.pythonhosted.org/packages/ec/5e/88854e28b3cb1cd5439d13299bde21b85be810d8d43f3547d72094e05eba/torch-1.13.0-cp37-cp37m-win_amd64.whl (167.3MB)ERROR: Exception:Traceback (most recent call last):File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_vendor\urllib3\response.py", line 397, in _error_catcheryieldFile "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_vendor\urllib3\response.py", line 479, in readdata = self._fp.read(amt)File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_vendor\cachecontrol\filewrapper.py", line 62, in readdata = self.__fp.read(amt)File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 457, in readn = self.readinto(b)File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\http\client.py", line 501, in readinton = self.fp.readinto(b)File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\socket.py", line 589, in readintoreturn self._sock.recv_into(b)File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 1071, in recv_intoreturn self.read(nbytes, buffer)File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 929, in readreturn self._sslobj.read(len, buffer)socket.timeout: The read operation timed outDuring handling of the above exception, another exception occurred:Traceback (most recent call last):File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_internal\cli\base_command.py", line 188, in mainstatus = self.run(options, args)File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_internal\commands\install.py", line 345, in runresolver.resolve(requirement_set)File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_internal\legacy_resolve.py", line 196, in resolveself._resolve_one(requirement_set, req)File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_internal\legacy_resolve.py", line 359, in _resolve_oneabstract_dist = self._get_abstract_dist_for(req_to_install)File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_internal\legacy_resolve.py", line 307, in _get_abstract_dist_forself.require_hashesFile "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_internal\operations\prepare.py", line 199, in prepare_linked_requirementprogress_bar=self.progress_barFile "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_internal\download.py", line 1064, in unpack_urlprogress_bar=progress_barFile "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_internal\download.py", line 924, in unpack_http_urlprogress_bar)File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_internal\download.py", line 1152, in _download_http_url_download_url(resp, link, content_file, hashes, progress_bar)File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_internal\download.py", line 861, in _download_urlhashes.check_against_chunks(downloaded_chunks)File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_internal\utils\hashes.py", line 75, in check_against_chunksfor chunk in chunks:File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_internal\download.py", line 829, in written_chunksfor chunk in chunks:File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_internal\utils\ui.py", line 156, in iterfor x in it:File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_internal\download.py", line 818, in resp_readdecode_content=False):File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_vendor\urllib3\response.py", line 531, in streamdata = self.read(amt=amt, decode_content=decode_content)File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_vendor\urllib3\response.py", line 496, in readraise IncompleteRead(self._fp_bytes_read, self.length_remaining)File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\contextlib.py", line 130, in __exit__self.gen.throw(type, value, traceback)File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\pip\_vendor\urllib3\response.py", line 402, in _error_catcherraise ReadTimeoutError(self._pool, None, 'Read timed out.')pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.WARNING: You are using pip version 19.2.3, however version 23.0 is available.You should consider upgrading via the 'python -m pip install --upgrade pip' command.

2.使用pip命令安装

从上面我们可以看到whl文件的下载地址以及文件大小,具体为:

https://files.pythonhosted.org/packages/ec/5e/88854e28b3cb1cd5439d13299bde21b85be810d8d43f3547d72094e05eba/torch-1.13.0-cp37-cp37m-win_amd64.whl (167.3MB)

我们按照这个地址将文件下载下来,然后使用pip命令安装。

其他版本Torch和Torchvision地址:https://download.pytorch.org/whl/torch_stable.html

(1)将torch-1.13.0-cp37-cp37m-win_amd64.whl 文件复制到编译器路径下

具体地址为:C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Scripts

定位到pycharm中使用的编译器地址:

这是默认的地址,如果是新设置项目的地址就选择对应的新地址。

(2)’win+r’ 输入cmd

输入:cd C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Scripts

指定安装路径

输入命令:pip install torch-1.13.0-cp37-cp37m-win_amd64.whl

安装成功以后:

3.运行测试

安装好之后,缺失的包提示波浪线不显示,表示安装成功了。

‘import torch_optimizer as optim改为‘import torch.optim as optim’

from torchsummary import summary中的torchsummary单独安装

补充:torch和torchvision版本对应关系:以win py37m 为例

torch1.12.0–torchvision-0.13.0

torch1.12.1–torchvision-0.13.1

torch1.13.0–torchvision-0.14.0

torch1.13.1–torchvision-0.14.1