最近準備入深度學(xué)習的坑衣吠,作為機器學(xué)習小白茶敏,決定還是從流行的Google的開源機器學(xué)習框架Tensorflow入手。Tensorflow原本只支持linux和mac OS, 但是最近Google宣布支持Windows系統(tǒng)缚俏。由于本人天生懶散惊搏,決定不去折騰ubuntu,一把跳進win10裝Tensorflow的大坑(都是淚)。本文目的在于記錄自己在win10下安裝Tensorflow的歷程忧换。好了廢話不多說恬惯,直接進入正題。
以下是win10配置Tensorflow所需要的工具和軟件包:
- Rapid Environment Editor(環(huán)境變量編輯器):鑒于后面的安裝過程需要修改很多環(huán)境變量亚茬,所以最好安裝一下它酪耳,會方便許多。
- Microsoft Visual Studio 2015 Community Edition: 用于其 C/C++編譯器(而不是 IDE)和 SDK刹缝,選擇該確定的版本是因為它是 CUDA 8.0.61 所支持的 Windows 編譯器碗暗。
- Anaconda3 (64-bit) + Python 3.6 (Anaconda3-4.4.0): anaconda指的是一個開源的Python發(fā)行版本颈将,其包含了conda、Python等180多個科學(xué)包及其依賴項言疗,是一個優(yōu)秀的集成開發(fā)環(huán)境晴圾。(python3.6也是最近才支持的)
- cuda_8.0.44(64bit): CUDA(Compute Unified Device Architecture),是顯卡廠商NVIDIA 推出的通用并行計算架構(gòu)洲守,該架構(gòu)使GPU能夠解決復(fù)雜的計算問題疑务。在深度學(xué)習中我們需要GPU的并行計算能力來加速深度學(xué)習算法。
- cudnn_8.0(64bit): 用來進一步加速深度神經(jīng)網(wǎng)絡(luò)的計算梗醇。
- DXSDK_Jun10.exe: 微軟的DirectX SDK工具包,不安裝它的話撒蟀,后面編譯CUDA_Samples是沒法成功的叙谨。
配置Tensorflow參考的一些文檔和網(wǎng)站:
- Tensorflow的Github項目地址: https://github.com/tensorflow
- Tensorflow官方網(wǎng)站: https://www.tensorflow.org
- Tensorflow中文社區(qū):http://www.tensorfly.cn
- Phil Ferriere 在 GitHub 上發(fā)布的教程: https://github.com/philferriere/dlwin
- 此外我還查找了大量的博客以及StackOverFlow上的問答,在此就不一一列舉了保屯。
安裝Rapid Environment Editor
下載網(wǎng)址:https://www.rapidee.com/en/download手负。
安裝完成后,默認界面是英文的姑尺,到設(shè)置里面改為中文竟终。啟動的時候,設(shè)置管理員啟動吧切蟋,不然沒法更改系統(tǒng)環(huán)境變量卢厂。
安裝CUDA
1. 安裝VS2015
下載地址:
https://www.visualstudio.com/zh-hans/vs/older-downloads/
我安裝的是Microsoft Visual Studio 2015 Community Edition钠怯,不用花錢。安裝過程就不贅述了,只需要裝Visual C++就行越除。順便說一句,Tensorflow貌似目前還不支持VS2017损搬,讀者要注意一下起惕。
2. 安裝DXSDK_Jun10.exe
下載地址:
https://pan.baidu.com/share/link?shareid=197164616&uk=369246564&fid=2918892502
安裝過程很簡單,無需多言堪夭。
3. 安裝 CUDA
下載地址:https://developer.nvidia.com/cuda-toolkit-archive
3.1 編譯示例程序
CUDA的示例程序在 C:\ProgramData\NVIDIA Corporation\CUDA Samples\v8.0 文件夾下愕把。VS2015則打開Samples_vs2015.sln,注意把解決方案配置更改為Release和x64森爽。
編譯整個解決方案恨豁,正常情況下是順利生成成功,如下圖:
3.2 最終檢查安裝是否正確
在C:\ProgramData\NVIDIA Corporation\CUDA Samples\v8.0\bin\win64\Release目錄下找到deviceQuery.exe文件拗秘。打開命令提示符cmd窗口圣絮,切換到到C:\ProgramData\NVIDIA Corporation\CUDA Samples\v8.0\bin\win64\Release目錄,輸入:deviceQuery.exe 雕旨,然后回車扮匠。得到如下結(jié)果:
只有得到如上圖所示的結(jié)果才說明CUDA安裝成功捧请。
安裝 Anaconda
下載地址 https://www.anaconda.com/download/,注意選擇Python 3.6 version棒搜,64-Bit Graphical Installer疹蛉。安裝完成后即可得到集成Python3.6的科學(xué)計算環(huán)境。安裝過程不復(fù)雜力麸,唯一要注意的點是在Advanced Installation Options中要把Add Anaconda to system PATH enviroment variable選上可款。
安裝 Tensorflow
如下圖在開始菜單欄找到Anaconda Prompt:
打開Anaconda Prompt,輸入
pip install tensorflow-gpu
回車克蚂,等待一段時間闺鲸,GPU版tensorflow就下載好啦!不過別著急埃叭,要能正常使用GPU加速功能摸恍,還有cudnn需要安裝。
安裝cudnn
下載地址:
https://developer.nvidia.com/cudnn
這個很簡單赤屋,下載后直接解壓縮立镶。加壓后是cuda文件夾,里面有三個文件夾bin, include, lib类早。把這三個文件夾的文件復(fù)制到到安裝CUDA的地方覆蓋對應(yīng)文件夾媚媒,默認文件夾在:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0。
安裝DXSDK_Jun10.exe
下載地址:https://pan.baidu.com/share/link?shareid=197164616&uk=369246564&fid=2918892502
下載后直接安裝即可涩僻。
測試tensorflow安裝是否正確
打開Anaconda Prompt缭召,輸入 python打開python環(huán)境,然后輸入以下代碼:
import tensorflow as tf
sess = tf.Session()
a = tf.constant(10)
b = tf.constant(22)
print(sess.run(a + b))
不報錯令哟,輸出結(jié)果為32恼琼,說明安裝成功。
注意輸入sess = tf.Session()回車后出現(xiàn)下圖結(jié)果說明gpu版Tensorflow安裝成功屏富,正在使用GPU加速晴竞。
可能出現(xiàn)的bug及其解決方案
缺少dll文件
import tensorflow報錯,錯誤如圖所示狠半。
新建python文件t.py, 代碼如下所示噩死。運行該文件,根據(jù)輸出結(jié)果修正錯誤神年。
import ctypes
import imp
import sys
def main():
try:
import tensorflow as tf
print("TensorFlow successfully installed.")
if tf.test.is_built_with_cuda():
print("The installed version of TensorFlow includes GPU support.")
else:
print("The installed version of TensorFlow does not include GPU support.")
sys.exit(0)
except ImportError:
print("ERROR: Failed to import the TensorFlow module.")
candidate_explanation = False
python_version = sys.version_info.major, sys.version_info.minor
print("\n- Python version is %d.%d." % python_version)
if not (python_version == (3, 5) or python_version == (3, 6)):
candidate_explanation = True
print("- The official distribution of TensorFlow for Windows requires "
"Python version 3.5 or 3.6.")
try:
_, pathname, _ = imp.find_module("tensorflow")
print("\n- TensorFlow is installed at: %s" % pathname)
except ImportError:
candidate_explanation = False
print("""
- No module named TensorFlow is installed in this Python environment. You may
install it using the command `pip install tensorflow`.""")
try:
msvcp140 = ctypes.WinDLL("msvcp140.dll")
except OSError:
candidate_explanation = True
print("""
- Could not load 'msvcp140.dll'. TensorFlow requires that this DLL be
installed in a directory that is named in your %PATH% environment
variable. You may install this DLL by downloading Microsoft Visual
C++ 2015 Redistributable Update 3 from this URL:
https://www.microsoft.com/en-us/download/details.aspx?id=53587""")
try:
cudart64_80 = ctypes.WinDLL("cudart64_80.dll")
except OSError:
candidate_explanation = True
print("""
- Could not load 'cudart64_80.dll'. The GPU version of TensorFlow
requires that this DLL be installed in a directory that is named in
your %PATH% environment variable. Download and install CUDA 8.0 from
this URL: https://developer.nvidia.com/cuda-toolkit""")
try:
nvcuda = ctypes.WinDLL("nvcuda.dll")
except OSError:
candidate_explanation = True
print("""
- Could not load 'nvcuda.dll'. The GPU version of TensorFlow requires that
this DLL be installed in a directory that is named in your %PATH%
environment variable. Typically it is installed in 'C:\Windows\System32'.
If it is not present, ensure that you have a CUDA-capable GPU with the
correct driver installed.""")
cudnn5_found = False
try:
cudnn5 = ctypes.WinDLL("cudnn64_5.dll")
cudnn5_found = True
except OSError:
candidate_explanation = True
print("""
- Could not load 'cudnn64_5.dll'. The GPU version of TensorFlow
requires that this DLL be installed in a directory that is named in
your %PATH% environment variable. Note that installing cuDNN is a
separate step from installing CUDA, and it is often found in a
different directory from the CUDA DLLs. You may install the
necessary DLL by downloading cuDNN 5.1 from this URL:
https://developer.nvidia.com/cudnn""")
cudnn6_found = False
try:
cudnn = ctypes.WinDLL("cudnn64_6.dll")
cudnn6_found = True
except OSError:
candidate_explanation = True
if not cudnn5_found or not cudnn6_found:
print()
if not cudnn5_found and not cudnn6_found:
print("- Could not find cuDNN.")
elif not cudnn5_found:
print("- Could not find cuDNN 5.1.")
else:
print("- Could not find cuDNN 6.")
print("""
The GPU version of TensorFlow requires that the correct cuDNN DLL be installed
in a directory that is named in your %PATH% environment variable. Note that
installing cuDNN is a separate step from installing CUDA, and it is often
found in a different directory from the CUDA DLLs. The correct version of
cuDNN depends on your version of TensorFlow:
* TensorFlow 1.2.1 or earlier requires cuDNN 5.1. ('cudnn64_5.dll')
* TensorFlow 1.3 or later requires cuDNN 6. ('cudnn64_6.dll')
You may install the necessary DLL by downloading cuDNN from this URL:
https://developer.nvidia.com/cudnn""")
if not candidate_explanation:
print("""
- All required DLLs appear to be present. Please open an issue on the
TensorFlow GitHub page: https://github.com/tensorflow/tensorflow/issues""")
sys.exit(-1)
if __name__ == "__main__":
main()