windows 10 安装 pytorch 1.7.1

1 查看是否有GPU

 下载和安装 Python 3.8

 下载和安装 PyCharm

2 下载 Anaconda

https://www.anaconda.com/

https://www.anaconda.com/products/individual

https://repo.anaconda.com/archive/Anaconda3-2020.11-Windows-x86_64.exe

3 安装 Anaconda

 

 

 

  • Anaconda Navigator :用于管理工具包和环境的图形用户界面,后续涉及的众多管理命令也可以在 Navigator 中手工实现。
  • Jupyter notebook :基于web的交互式计算环境,可以编辑易于人们阅读的文档,用于展示数据分析的过程。
  • qtconsole :一个可执行 IPython 的仿终端图形界面程序,相比 Python Shell 界面,qtconsole 可以直接显示代码生成的图形,实现多行代码输入执行,以及内置许多有用的功能和函数。
  • Spyder :一个使用Python语言、跨平台的、科学运算集成开发环境。

4 打开Anaconda

Run as administrator

5 管理虚环境

创建虚拟环境,为自己的程序安装单独的虚拟环境.
创建一个名称为 myenvpy38 的虚拟环境并指定python版本为3.8
conda create -n myenvpy38 python=3.8

environment location: E:EprogramfilesAnaconda3envsmyenvpy38

其中 E:EprogramfilesAnaconda3 是anaconda的安装路径。


切换虚拟环境
切换到这个环境, 用activae命令,后面加上要切换的环境名称
conda activate myenvpy38

查看所有的环境
如果忘记了名称我们可以先用
conda env list


# To deactivate an active environment, use
# conda deactivate

conda env list

conda list

安装第三方包
 conda install packageName
 或者
 pip install packageName


卸载第三方包
 conda remove packageName
  或者
  pip uninstall packageName


6 安装PyTorch

以下步骤安装不成功:

https://pytorch.org/

 

conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch



The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    cudatoolkit-10.2.89        |       h74a9793_1       317.2 MB
    libuv-1.40.0               |       he774522_0         255 KB
    lz4-c-1.9.3                |       h2bbff1b_0         131 KB
    mkl-service-2.3.0          |   py38h196d8e1_0          47 KB
    ninja-1.10.2               |   py38h6d14046_0         247 KB
    pillow-8.1.0               |   py38h4fa10fc_0         664 KB
    pytorch-1.7.1              |py3.8_cuda102_cudnn7_0       768.1 MB  pytorch
    torchaudio-0.7.2           |             py38         2.7 MB  pytorch
    torchvision-0.8.2          |       py38_cu102         7.2 MB  pytorch
    ------------------------------------------------------------
                                           Total:        1.07 GB

The following NEW packages will be INSTALLED:

  blas               pkgs/main/win-64::blas-1.0-mkl
  cudatoolkit        pkgs/main/win-64::cudatoolkit-10.2.89-h74a9793_1
  freetype           pkgs/main/win-64::freetype-2.10.4-hd328e21_0
  intel-openmp       pkgs/main/win-64::intel-openmp-2020.2-254
  jpeg               pkgs/main/win-64::jpeg-9b-hb83a4c4_2
  libpng             pkgs/main/win-64::libpng-1.6.37-h2a8f88b_0
  libtiff            pkgs/main/win-64::libtiff-4.1.0-h56a325e_1
  libuv              pkgs/main/win-64::libuv-1.40.0-he774522_0
  lz4-c              pkgs/main/win-64::lz4-c-1.9.3-h2bbff1b_0
  mkl                pkgs/main/win-64::mkl-2020.2-256
  mkl-service        pkgs/main/win-64::mkl-service-2.3.0-py38h196d8e1_0
  mkl_fft            pkgs/main/win-64::mkl_fft-1.2.0-py38h45dec08_0
  mkl_random         pkgs/main/win-64::mkl_random-1.1.1-py38h47e9c7a_0
  ninja              pkgs/main/win-64::ninja-1.10.2-py38h6d14046_0
  numpy              pkgs/main/win-64::numpy-1.19.2-py38hadc3359_0
  numpy-base         pkgs/main/win-64::numpy-base-1.19.2-py38ha3acd2a_0
  olefile            pkgs/main/noarch::olefile-0.46-py_0
  pillow             pkgs/main/win-64::pillow-8.1.0-py38h4fa10fc_0
  pytorch            pytorch/win-64::pytorch-1.7.1-py3.8_cuda102_cudnn7_0
  six                pkgs/main/win-64::six-1.15.0-py38haa95532_0
  tk                 pkgs/main/win-64::tk-8.6.10-he774522_0
  torchaudio         pytorch/win-64::torchaudio-0.7.2-py38
  torchvision        pytorch/win-64::torchvision-0.8.2-py38_cu102
  typing_extensions  pkgs/main/noarch::typing_extensions-3.7.4.3-py_0
  xz                 pkgs/main/win-64::xz-5.2.5-h62dcd97_0
  zstd               pkgs/main/win-64::zstd-1.4.5-h04227a9_0


Proceed ([y]/n)? y


Downloading and Extracting Packages
torchaudio-0.7.2     | 2.7 MB    | ######5                                                                      |   9%
pytorch-1.7.1        | 768.1 MB  |                                                                                    |   0%
torchvision-0.8.2    | 7.2 MB    | #2                                                                                 |   2%
ninja-1.10.2         | 247 KB    | ################################################################################## | 100%
mkl-service-2.3.0    | 47 KB     | ################################################################################## | 100%
libuv-1.40.0         | 255 KB    | ################################################################################## | 100%
pillow-8.1.0         | 664 KB    | ################################################################################## | 100%
cudatoolkit-10.2.89  | 317.2 MB  | ###3                                                                               |   4%
lz4-c-1.9.3          | 131 KB    | ################################################################################## | 100%

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://conda.anaconda.org/pytorch/win-64/torchaudio-0.7.2-py38.tar.bz2>
Elapsed: -

An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://conda.anaconda.org/pytorch/win-64/pytorch-1.7.1-py3.8_cuda102_cudnn7_0.tar.bz2>
Elapsed: -

An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://conda.anaconda.org/pytorch/win-64/torchvision-0.8.2-py38_cu102.tar.bz2>
Elapsed: -

An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.

("Connection broken: ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)", ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))


(myenvpy38) E:EprogramfilesAnaconda3myenv>


改变安装策略:
1 查看显卡对应的 CUDA
C盘搜索 nvcuda64.dll,右键,属性

 2 下载 cuda_11.0.3

https://developer.nvidia.com/cuda-toolkit-archive

http://developer.download.nvidia.com/compute/cuda/11.0.3/local_installers/cuda_11.0.3_451.82_win10.exe

文件3G左右,用迅雷下载比较快

3 安装 cuda_11.0.3

默认都是必须安装在C盘,超过4.5GB空间。自定义安装的时候可以选择路径 e:Eprogramfilescuda11dev,大部分文件仍然安装到C盘了(C:Program FilesNVIDIA GPU Computing Toolkit)

检查是否安装成功

e:Eprogramfilescuda11devbin>nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Wed_Jul_22_19:09:35_Pacific_Daylight_Time_2020
Cuda compilation tools, release 11.0, V11.0.221
Build cuda_11.0_bu.relgpu_drvr445TC445_37.28845127_0

e:Eprogramfilescuda11devin>

 

4 下载与 cuda 相应的 cudnn

https://developer.nvidia.com/rdp/cudnn-archive

https://developer.nvidia.com/compute/machine-learning/cudnn/secure/8.0.4/11.0_20200923/cudnn-11.0-windows-x64-v8.0.4.30.zip

解压 cudnn-11.0-windows-x64-v8.0.4.30.zip

前面安装的cuda的路径下也有这三个对应的文件夹(bin,include,lib),我们要做的就是用cudnn的三个文件夹覆盖cuda中对应的三个文件夹.直接粘过去就行了!

测试是否将cudnn安装好
首先进入CUDA的安装路径 -> extras -> demo_suite,  E:Eprogramfilescuda11devextrasdemo_suite 里面有两个测试程序,一个是bandwidthTest.exe,一个是deviceQuery.exe

然后可以在demo_suite这个文件夹下打开cmd,运行那两个exe,结果如下图

E:Eprogramfilescuda11devextrasdemo_suite>bandwidthTest.exe
[CUDA Bandwidth Test] - Starting...
Running on...

 Device 0: GeForce GTX 1050
 Quick Mode

 Host to Device Bandwidth, 1 Device(s)
 PINNED Memory Transfers
   Transfer Size (Bytes)        Bandwidth(MB/s)
   33554432                     12564.8

 Device to Host Bandwidth, 1 Device(s)
 PINNED Memory Transfers
   Transfer Size (Bytes)        Bandwidth(MB/s)
   33554432                     12848.8

 Device to Device Bandwidth, 1 Device(s)
 PINNED Memory Transfers
   Transfer Size (Bytes)        Bandwidth(MB/s)
   33554432                     95124.9

Result = PASS

NOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled.

E:Eprogramfilescuda11devextrasdemo_suite>deviceQuery.exe
deviceQuery.exe Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 1 CUDA Capable device(s)

Device 0: "GeForce GTX 1050"
  CUDA Driver Version / Runtime Version          11.0 / 11.0
  CUDA Capability Major/Minor version number:    6.1
  Total amount of global memory:                 4096 MBytes (4294967296 bytes)
  ( 5) Multiprocessors, (128) CUDA Cores/MP:     640 CUDA Cores
  GPU Max Clock rate:                            1493 MHz (1.49 GHz)
  Memory Clock rate:                             3504 Mhz
  Memory Bus Width:                              128-bit
  L2 Cache Size:                                 524288 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384)
  Maximum Layered 1D Texture Size, (num) layers  1D=(32768), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(32768, 32768), 2048 layers
  Total amount of constant memory:               zu bytes
  Total amount of shared memory per block:       zu bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  2048
  Maximum number of threads per block:           1024
  Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
  Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)
  Maximum memory pitch:                          zu bytes
  Texture alignment:                             zu bytes
  Concurrent copy and kernel execution:          Yes with 5 copy engine(s)
  Run time limit on kernels:                     Yes
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Disabled
  CUDA Device Driver Mode (TCC or WDDM):         WDDM (Windows Display Driver Model)
  Device supports Unified Addressing (UVA):      Yes
  Device supports Compute Preemption:            Yes
  Supports Cooperative Kernel Launch:            Yes
  Supports MultiDevice Co-op Kernel Launch:      No
  Device PCI Domain ID / Bus ID / location ID:   0 / 1 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 11.0, CUDA Runtime Version = 11.0, NumDevs = 1, Device0 = GeForce GTX 1050
Result = PASS

5 安装PyTorch

=====================================================

 conda activate myenvpy38

镜像源配置一下, 仍然特别慢
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --set show_channel_urls yes

conda install pytorch torchvision torchaudio cudatoolkit=11.0 -c pytorch

 =====================================================

在下载的过程中下载torch1.7.1的时候比较慢,下载的过程中还会超时,故直接拷贝下载地址下载whl文件,安装whl文件。

单独下载:

https://download.pytorch.org/whl/torch_stable.html

https://download.pytorch.org/whl/cu110/torchvision-0.8.2%2Bcu110-cp38-cp38-win_amd64.whl

https://download.pytorch.org/whl/cu110/torch-1.7.1%2Bcu110-cp38-cp38-win_amd64.whl

https://download.pytorch.org/whl/torchaudio-0.7.2-cp38-none-win_amd64.whl

 conda activate myenvpy38

pip --default-timeout=1000 install -U numpy  -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

pip --default-timeout=1000 install -U matplotlib.pyplot -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip --default-timeout=1000 install -U matplotlib  -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
 
pip --default-timeout=1000 install -U pandas -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

pip --default-timeout=1000 install -U sklearn -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

pip --default-timeout=1000 install -U typing-extensions -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

安装有先后顺序,先torch

 E:EprogramfilesAnaconda3envsmyenvpy38>pip install "D:software orch-1.7.1+cu110-cp38-cp38-win_amd64.whl"

  E:EprogramfilesAnaconda3envsmyenvpy38>pip install D:software orchaudio-0.7.2-cp38-none-win_amd64.whl

 E:EprogramfilesAnaconda3envsmyenvpy38>pip install "D:software orchvision-0.8.2+cu110-cp38-cp38-win_amd64.whl"


REF
https://blog.csdn.net/qq_36306288/article/details/111243361

https://blog.csdn.net/weixin_42144294/article/details/111624608
https://www.cnblogs.com/chenyameng/p/14273935.html

https://blog.csdn.net/adong6561975/article/details/106548396/


原文地址:https://www.cnblogs.com/emanlee/p/14332287.html