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

最近準(zhǔn)備入深度學(xué)習(xí)的坑,作為機器學(xué)習(xí)小白,決定還是從流行的Google的開源機器學(xué)習(xí)框架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é)習(xí)中我們需要GPU的并行計算能力來加速深度學(xué)習(xí)算法。
  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。
安裝完成后,默認(rèn)界面是英文的,到設(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)文件夾,默認(rèn)文件夾在: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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容