參考網址:https://www.tensorflow.org/install/pip (或要VPN)
為了減少不必要的錯誤:
確定電腦是Nvidia 顯卡(下稱:N卡),安裝前先升級驅動
各N卡計算力查詢:https://developer.nvidia.com/cuda-gpus
PS:不是N卡就別折騰CUDA、cuDNN、Tensorflow-GPU,只能安裝Tensorflow-CPU
PS:老CPU建議從低版本開始測試安裝,Tensorflow使用較新的指令集
1.下載Microsoft Visual C++ 2015 Redistributable Update 3(已安裝更高級版或安裝VS2015或VS2017可以跳過)
https://www.microsoft.com/zh-CN/download/details.aspx?id=53587
安裝.NET Framework
https://dotnet.microsoft.com/download/dotnet-framework
2.啟用長路徑。(Win10)
https://superuser.com/a/1119980
3. 安裝Python3.7.4,在安裝界面上勾選添加環境變量復選框
https://www.python.org/downloads/
PS:tensorflow僅支持沒有那么新的Python,查看https://tensorflow.google.cn/install/pip?lang=python3#package-location
PS:參考:https://tensorflow.google.cn/install/source_windows(得知官方tensorflow 1.x完全支持Python v3.5.x-v3.6.x)
修改pip源(永久)
新建C:\Users\Administrator\pip\pip.ini,添加代碼:
[global] timeout=6000 index-url=https://pypi.tuna.tsinghua.edu.cn/simple trusted-host=pypi.tuna.tsinghua.edu.cn
批量更新包(可選)
參考:http://blog.sciencenet.cn/home.php?mod=space&uid=478347&do=blog&id=1115281
import pip # pip V10.0.0以上版本需要導入下面的包 from pip._internal.utils.misc import get_installed_distributions from subprocess import call for dist in get_installed_distributions(): call('python -m pip install --upgrade --user ' + dist.project_name, shell=True)
保存到D:\upgrade.py,執行命令更新
python D:\upgrade.py
PS:numpy不要裝得太新(v1.17.0+),不然有很多警告
python -m pip install numpy==1.16.4
4.cuda 10.0和cudnn 7.6.1(不是N卡,跳過此步)
https://developer.nvidia.com/cuda-toolkit-archive
https://developer.nvidia.com/rdp/cudnn-archive (注意對應cuda版本)
cudnn安裝方法參考:https://jingyan.baidu.com/article/39810a236d660bb636fda6d4.html(簡單來說就是解壓cudnn,將文件粘貼到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0)
添加環境變量到PATH
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\include
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\lib\x64
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\extras\CUPTI\libx64
PS:我的電腦不支持10.1,當運行hello world時就會報錯
ImportError: Could not find 'cudart64_100.dll'. TensorFlow requires that this DLL be installed in a directory that is named in your %PATH% environment variable. Download and install CUDA 10.0 from this URL: https://developer.nvidia.com/cuda-90-download-archive
5. 安裝tensorflow 1.14(選其一)(注意對應的Python版本)
安裝GPU版
pip install tensorflow-gpu==1.14
安裝CPU版
pip install tensorflow==1.14

PS:當遇到這個情況時,可能是執行升級時失敗
參考:https://ask.csdn.net/questions/942629?sort=id
我是pip升和setuptools級失敗,所以會提示這個

進入C:\Users\Administrator\AppData\Roaming\Python\Python37\site-packages刪除對應的pip-19.3.1.dist-info和setuptools-42.0.2.dist-info文件夾,再執行一次單獨升級(再遇到也同樣操作)(PS:估計是批量升級出錯)
PS:安裝失敗時,將C:\Program Files\Python37的權限設置為完全控制

如遇到“ImportError: DLL load failed: 動態鏈接庫(DLL)初始化例程失敗。”,則說明你的硬件不支持該版本的TensorFlow,需要安裝低版本
參考:https://blog.csdn.net/baiduauto1/article/details/96578662
參考:https://blog.csdn.net/lchzh1994/article/details/81223726
6.測試
import tensorflow as tf h = tf.constant("Hello") w = tf.constant(" World!") hw = h + w # 警告信息提示要將 tf.Session() 替換為 tf.compat.v1.Session() with tf.compat.v1.Session() as sess: ans = sess.run(hw) print(ans)
屏蔽GPU輸出信息可以添加上這句:
參考:https://blog.csdn.net/dcrmg/article/details/80029741
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
TF_CPP_MIN_LOG_LEVEL 0 :輸出所有信息(默認值) 1 :屏蔽通知信息 2 :屏蔽通知信息和警告信息 3 :屏蔽通知信息、警告信息和報錯信息
附:
當使用VSCode開發時可以按Ctrl+Shift+P,輸入python->選擇解析器,會顯示所有環境(conda、venv等),選擇任何一個作為解析器
關於Anaconda安裝
只需要將上面的python安裝替換為Anaconda(好像會自帶python,沒測試)
1.安裝Anaconda 2019.07(注意對應Python版本)
https://www.anaconda.com/distribution/
清華大學鏡像
https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
# 指定升級某一個庫,例如 pandas conda update pandas # 升級所有可升級的庫 conda update --all
2.安裝OpenSSL v1.1.1c,不然Conda安裝時可能會出下面的錯誤
http://slproweb.com/products/Win32OpenSSL.html

3.打開CMD,添加Conda源(可以不要https)
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
PS:可以直接打開C:\Users\Administrator\.condarc編輯:
channels: - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/ - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ show_channel_urls: true
7. 安裝tensorflow 1.14.0(選其一)
安裝GPU版
conda install tensorflow-gpu==1.14.0
安裝CPU版
conda install tensorflow==1.14.0
親測:
Win7 旗艦版(台式機+獨顯)
python-3.7.3-amd64.exe
Anaconda3-2019.07-Windows-x86_64.exe(PS:控制面板-程序和功能看到:Anaconda3 2019.07 (Python 3.7.3 64-bit))
Win64OpenSSL-1_1_1c.exe
cuda_10.1.168_425.25_windows.exe
cudnn-10.1-windows7-x64-v7.5.0.56.zip
TensorFlow-GPU 1.14.0
Win10專業版(台式機+獨立N卡)
python-3.7.3-amd64.exe
Anaconda3-2019.07-Windows-x86_64.exe
cuda_10.0.130_411.31_win10
cudnn-10.0-windows10-x64-v7.6.0.64.zip
TensorFlow-GPU 1.13.1
同一台機
python-3.7.4-amd64.exe
cuda_10.0.130_411.31_win10
cudnn-10.0-windows10-x64-v7.6.1.34.zip
TensorFlow-GPU 1.14.0
