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

最近准备入深度学习的坑,作为机器学习小白,决定还是从流行的Google的开源机器学习框架Tensorflow入手。Tensorflow原本只支持linux和mac OS, 但是最近Google宣布支持Windows系统。由于本人天生懒散,决定不去折腾ubuntu,一把跳进win10装Tensorflow的大坑(都是泪)。本文目的在于记录自己在win10下安装Tensorflow的历程。好了废话不多说,直接进入正题。

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

  1. Rapid Environment Editor(环境变量编辑器):鉴于后面的安装过程需要修改很多环境变量,所以最好安装一下它,会方便许多。
  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发行版本,其包含了conda、Python等180多个科学包及其依赖项,是一个优秀的集成开发环境。(python3.6也是最近才支持的)
  4. cuda_8.0.44(64bit): CUDA(Compute Unified Device Architecture),是显卡厂商NVIDIA 推出的通用并行计算架构,该架构使GPU能够解决复杂的计算问题。在深度学习中我们需要GPU的并行计算能力来加速深度学习算法。
  5. cudnn_8.0(64bit): 用来进一步加速深度神经网络的计算。
  6. DXSDK_Jun10.exe: 微软的DirectX SDK工具包,不安装它的话,后面编译CUDA_Samples是没法成功的。

配置Tensorflow参考的一些文档和网站:

安装Rapid Environment Editor

下载网址:https://www.rapidee.com/en/download
安装完成后,默认界面是英文的,到设置里面改为中文。启动的时候,设置管理员启动吧,不然没法更改系统环境变量。

安装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。

编译整个解决方案,正常情况下是顺利生成成功,如下图:

《win10配置tensorflow GPU版 ( VS2015 + Acconda3 + CUDA8.0 + cuDNN v5.1 )》 示例程序生成成功

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 ,然后回车。得到如下结果:

《win10配置tensorflow GPU版 ( VS2015 + Acconda3 + CUDA8.0 + cuDNN v5.1 )》 检查CUDA安装结果

只有得到如上图所示的结果才说明CUDA安装成功。

安装 Anaconda

下载地址 https://www.anaconda.com/download/,注意选择Python 3.6 version,64-Bit Graphical Installer。安装完成后即可得到集成Python3.6的科学计算环境。安装过程不复杂,唯一要注意的点是在Advanced Installation Options中要把Add Anaconda to system PATH enviroment variable选上。

安装 Tensorflow

如下图在开始菜单栏找到Anaconda Prompt:

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

打开Anaconda Prompt,输入

pip install tensorflow-gpu

回车,等待一段时间,GPU版tensorflow就下载好啦!不过别着急,要能正常使用GPU加速功能,还有cudnn需要安装。

安装cudnn

下载地址:
https://developer.nvidia.com/cudnn
这个很简单,下载后直接解压缩。加压后是cuda文件夹,里面有三个文件夹bin, include, lib。把这三个文件夹的文件复制到到安装CUDA的地方覆盖对应文件夹,默认文件夹在: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环境,然后输入以下代码:

import tensorflow as tf
sess = tf.Session()
a = tf.constant(10)
b = tf.constant(22)
print(sess.run(a + b))

不报错,输出结果为32,说明安装成功。
注意输入sess = tf.Session()回车后出现下图结果说明gpu版Tensorflow安装成功,正在使用GPU加速。

《win10配置tensorflow GPU版 ( VS2015 + Acconda3 + CUDA8.0 + cuDNN v5.1 )》 测试tensorflow安装是否正确

可能出现的bug及其解决方案

缺少dll文件

《win10配置tensorflow GPU版 ( VS2015 + Acconda3 + CUDA8.0 + cuDNN v5.1 )》 缺少DLL文件

import tensorflow报错,错误如图所示。

新建python文件t.py, 代码如下所示。运行该文件,根据输出结果修正错误。

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()
    原文作者:怅惘银河
    原文地址: https://www.jianshu.com/p/fd27a586f8c2
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞