win10配置tensorflow GPU版 ( VS2015 + Acconda3 + CUDA8.0 + cuDNN v5.1 )

最近準備入深度學(xué)習的坑衣吠,作為機器學(xué)習小白茶敏,決定還是從流行的Google的開源機器學(xué)習框架Tensorflow入手。Tensorflow原本只支持linux和mac OS, 但是最近Google宣布支持Windows系統(tǒng)缚俏。由于本人天生懶散惊搏,決定不去折騰ubuntu,一把跳進win10裝Tensorflow的大坑(都是淚)。本文目的在于記錄自己在win10下安裝Tensorflow的歷程忧换。好了廢話不多說恬惯,直接進入正題。

以下是win10配置Tensorflow所需要的工具和軟件包:

  1. Rapid Environment Editor(環(huán)境變量編輯器):鑒于后面的安裝過程需要修改很多環(huán)境變量亚茬,所以最好安裝一下它酪耳,會方便許多。
  2. Microsoft Visual Studio 2015 Community Edition: 用于其 C/C++編譯器(而不是 IDE)和 SDK刹缝,選擇該確定的版本是因為它是 CUDA 8.0.61 所支持的 Windows 編譯器碗暗。
  3. Anaconda3 (64-bit) + Python 3.6 (Anaconda3-4.4.0): anaconda指的是一個開源的Python發(fā)行版本颈将,其包含了conda、Python等180多個科學(xué)包及其依賴項言疗,是一個優(yōu)秀的集成開發(fā)環(huán)境晴圾。(python3.6也是最近才支持的)
  4. cuda_8.0.44(64bit): CUDA(Compute Unified Device Architecture),是顯卡廠商NVIDIA 推出的通用并行計算架構(gòu)洲守,該架構(gòu)使GPU能夠解決復(fù)雜的計算問題疑务。在深度學(xué)習中我們需要GPU的并行計算能力來加速深度學(xué)習算法。
  5. cudnn_8.0(64bit): 用來進一步加速深度神經(jīng)網(wǎng)絡(luò)的計算梗醇。
  6. DXSDK_Jun10.exe: 微軟的DirectX SDK工具包,不安裝它的話撒蟀,后面編譯CUDA_Samples是沒法成功的叙谨。

配置Tensorflow參考的一些文檔和網(wǎng)站:

安裝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é)果:

檢查CUDA安裝結(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

打開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加速晴竞。

測試tensorflow安裝是否正確

可能出現(xiàn)的bug及其解決方案

缺少dll文件

缺少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()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末已维,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子已日,更是在濱河造成了極大的恐慌垛耳,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異堂鲜,居然都是意外死亡栈雳,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門缔莲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來哥纫,“玉大人,你說我怎么就攤上這事痴奏≈В” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵读拆,是天一觀的道長擅憔。 經(jīng)常有香客問我,道長檐晕,這世上最難降的妖魔是什么雕欺? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮棉姐,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘啦逆。我一直安慰自己伞矩,他們只是感情好,可當我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布夏志。 她就那樣靜靜地躺著乃坤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪沟蔑。 梳的紋絲不亂的頭發(fā)上湿诊,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天,我揣著相機與錄音瘦材,去河邊找鬼厅须。 笑死,一個胖子當著我的面吹牛食棕,可吹牛的內(nèi)容都是我干的朗和。 我是一名探鬼主播,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼簿晓,長吁一口氣:“原來是場噩夢啊……” “哼眶拉!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起憔儿,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤忆植,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體朝刊,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡耀里,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了坞古。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片备韧。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖痪枫,靈堂內(nèi)的尸體忽然破棺而出织堂,到底是詐尸還是另有隱情,我是刑警寧澤奶陈,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布易阳,位于F島的核電站,受9級特大地震影響吃粒,放射性物質(zhì)發(fā)生泄漏潦俺。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一徐勃、第九天 我趴在偏房一處隱蔽的房頂上張望事示。 院中可真熱鬧,春花似錦僻肖、人聲如沸肖爵。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽劝堪。三九已至,卻和暖如春揉稚,著一層夾襖步出監(jiān)牢的瞬間秒啦,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工搀玖, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留余境,地道東北人。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓巷怜,卻偏偏與公主長得像葛超,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子延塑,可洞房花燭夜當晚...
    茶點故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容