安裝 tensorflow,pytorch GPU 版簡便好使的方法

網上其它教程說的去NVIDIA 官網下載cuda,註冊下載cudnn,用下面這種方法的教程好像很少,個人覺得下面方法特別適合需要裝多個conda環境的情況。

1.查看電腦NVIDIA 驅動的cuda版本,進入NVIDIA 控制面板,選擇系統資訊,在conda環境實際安裝cuda時只能小於等於這個版本,如果需要更高版本,可以在NVIDIA 官網下載更新驅動,鏈接:

//www.nvidia.cn/Download/index.aspx?lang=cn

2.創建conda環境:

conda create -n pytorch python=3.7

conda更換為中國源,更改:C:\Users\用戶名.condarc文件

channels:
  #- defaults
  - //mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - //mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - //mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  - //mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/win-64/
  - //mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
ssl_verify: true
channel_priority: flexible

defaults最好和這裡一樣注釋掉

使用命令conda activate pytorch 進入conda虛擬環境後,在conda環境里安裝cuda,但版本必須低於或等於上面查詢的版本,這樣就不用做網上其它教程說的去NVIDIA 官網下載cuda,註冊下載cudnn,直接使用命令安裝NVIDIA 的SDK10.1版本:

conda install cudatoolkit=10.1

安裝NVIDIA 深度學習軟體包7.6版本

conda install cudnn=7.6

3.更換pip源,在conda虛擬環境使用pip安裝numpy, six(網上有的教程說有這一步才不會報錯,那就安裝一下吧) 先將pip更換為中國源windows下,直接在user目錄中創建一個pip目錄,如:C:\Users\xx\pip,新建文件pip.ini,內容如下

[global] 
timeout = 60000
index-url = //pypi.tuna.tsinghua.edu.cn/simple
pip install numpy

4.進入pytorch官網

5.打開鏈接//download.pytorch.org/whl/torch_stable.html,我發現使用迅雷下載對應版本的torch和tourchvision速度很ok, IDM下不了,瀏覽器自帶下載很慢

6.使用pip離線安裝下載的包:cd進入到安裝包所在文件夾(建議桌面),然後pip install +文件名, eg:

pip install "torchvision-0.6.1+cu101-cp37-cp37m-win_amd64.whl"

注意,先裝torch,再裝torchvision

7.測試

import torch
torch.cuda.is_available()

返回True,安裝成功!

可以遨遊在神經網路的世界裡了…