关于在Windows上配置GPU版的TensorFlow (Anaconda环境)

和网上的大部分教程一样, 配置GPU版的TensorFlow大概需要以下几步.

首先要看本机的显卡型号.

在控制面板中的设备管理器, 检查本机的显卡是否支持Nvidia的CUDA, 

https://www.geforce.com/hardware/technology/cuda/supported-gpus

https://docs.nvidia.com/deploy/cuda-compatibility/index.html

以及计算能力是否满足官方文档中要求的3.5.

https://www.tensorflow.org/install/gpu

《关于在Windows上配置GPU版的TensorFlow (Anaconda环境)》

确认本机显卡可以支持CUDA且显卡计算能力达到要求后, 下载合适的CUDA以及cuDNN.

CUDA下载 https://developer.nvidia.com/cuda-toolkit-archive

《关于在Windows上配置GPU版的TensorFlow (Anaconda环境)》

cuDNN下载 https://developer.nvidia.com/rdp/cudnn-archive

《关于在Windows上配置GPU版的TensorFlow (Anaconda环境)》

一定注意cuDNN的版本要和CUDA相对应, 比如CUDA 9.0 (CUDA 9.2) 配 cuDNN v7.3.1, CUDA 8.0 配 cuDNN v7.1.4.

《关于在Windows上配置GPU版的TensorFlow (Anaconda环境)》

《关于在Windows上配置GPU版的TensorFlow (Anaconda环境)》 这里我忽略了警告, 继续完成安装, 并安装了后续的补丁包

《关于在Windows上配置GPU版的TensorFlow (Anaconda环境)》 这里可以不勾选Visual Studio Integration

楼主这里用的是CUDA 8.0, 在初始安装的时候会出现不兼容的警告, 但在安装了补丁包 (网页上对应版本的CUDA打开后可能会有不止一个下载项, 都下载下来,并顺序安装) 以后似乎最终没有影响.

安装完成后, 应出现两个变量.

《关于在Windows上配置GPU版的TensorFlow (Anaconda环境)》

添加环境变量. 在系统的环境变量Path里面加上以下四个路径,

《关于在Windows上配置GPU版的TensorFlow (Anaconda环境)》 这是楼主全部的环境变量, 除了Java 和 Git 的变量在这里用不到以外, 建议大家把Anaconda3的对应路径(三个)也添加进来. 如果缺少C:\Windows路径 (Java 和 Anaconda3中间的三条)的也最好补上, 否则在打开命令窗口的时候会出现一些不必要的报错.

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\x64

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\libnvvp

之后是安装cuDNN, 也没什么障碍, 从英伟达官网上注册账号后下载的cuDNN是一个压缩包, 解压后应该是

《关于在Windows上配置GPU版的TensorFlow (Anaconda环境)》

将每个文件夹下的文件, 分别拷贝至CUDA 安装目录的对应文件夹下(也就是上面添加环境变量的四个中的前三个路径).

一般的, CUDA 和 cuDNN的安装应该都不会出问题. 在命令窗或Anaconda Prompt中输入

nvcc –version (or nvcc -V), 应得到如下结果.

《关于在Windows上配置GPU版的TensorFlow (Anaconda环境)》

装好CUDA和cuDNN后, 配置Anaconda环境.

用命令 conda create –name tensorflow python=3.6 (也可以是3.5, 但3.7不确定)

或直接打开anaconda navigator, 在environment中直接create.

《关于在Windows上配置GPU版的TensorFlow (Anaconda环境)》 个人更喜欢这个

使用pip install tensorflow-gpu进行安装. 这里安装的tensorflow可能是最新版, 也就是有不兼容的风险, 若出现不兼容的报错现象, 非常正常. 

这里转载一个TensorFlow作者的文档.

————————————————————————————————————————

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.

# Licensed under the Apache License, Version 2.0 (the “License”);

# you may not use this file except in compliance with the License.

# You may obtain a copy of the License at

#    http://www.apache.org/licenses/LICENSE-2.0

#

# Unless required by applicable law or agreed to in writing, software

# distributed under the License is distributed on an “AS IS” BASIS,

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

# See the License for the specific language governing permissions and

# limitations under the License.

# ==============================================================================

“””A script for testing that TensorFlow is installed correctly on Windows.

The script will attempt to verify your TensorFlow installation, and print

suggestions for how to fix your installation.

“””

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.”)

  print(“””

WARNING! This script is no longer maintained!

=============================================

Since TensorFlow 1.4, the self-check has been integrated with TensorFlow itself,

and any missing DLLs will be reported when you execute the `import tensorflow`

statement. The error messages printed below refer to TensorFlow 1.3 and earlier,

and are inaccurate for later versions of TensorFlow.”””)

  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()

————————————————————————————————————————

将分割线中的代码复制粘贴到自己新建的一个py文件中, 运行该py文件. 可以针对输出结果采取措施. 比如楼主曾经遇到了Could not load ‘cudart64_80.dll’ (当时楼主使用的是CUDA 9.2, 后来不得不回退到CUDA 8.0) 和 Could not load ‘cudnn64_5.dll’ (楼主强行将cuDNN压缩包中的文件cudnn64_7.dll重命名为cudnn64_5.dll). 之后便遇到了版本不兼容的信息. 于是只好装老版的TensorFlow (1.2.1, 目前最新版为1.18.0).

WINDOWS: pip install –ignore-installed –upgrade https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-1.2.1-cp35-cp35m-win_amd64.whl (python 3.6环境的自行将35全部替换成36).

到此为止, GPU版的TensorFlow应该搭建完毕. 若仍然有问题, 参考github的TensorFlow的issue或去Nvidia的技术论坛中寻求帮助.

最终测试,

这里多说一句, 在TensorFlow环境下安装Jupyter的时候切不可想当然的安装最新版的Jupyter.

《关于在Windows上配置GPU版的TensorFlow (Anaconda环境)》
《关于在Windows上配置GPU版的TensorFlow (Anaconda环境)》

《关于在Windows上配置GPU版的TensorFlow (Anaconda环境)》 https://www.tensorflow.org/guide/using_gpu?hl=zh-cn

参考链接

[1] https://www.tensorflow.org/install/gpu

[2] http://www.mclover.cn/blog/index.php/archives/179.html

[3] https://evol4.wordpress.com/2017/09/02/install-tensorflow-1-2-1-for-python-3-5-on-ubuntu-16-cuda-8-0-cudnn-5-1/

[4] https://zhuanlan.zhihu.com/p/38223869

[5] https://blog.csdn.net/XCCCCZ/article/details/80385448

[6] https://cloud.tencent.com/developer/article/1165267

[7] https://zhuanlan.zhihu.com/p/27168325

[8] https://www.jianshu.com/p/00ac18eee943

[9] https://blog.csdn.net/sb19931201/article/details/53648615

[10] https://github.com/tensorflow/tensorflow/issues/10085

[11] http://blog.nitishmutha.com/tensorflow/2017/01/22/TensorFlow-with-gpu-for-windows.html

[12] https://github.com/tensorflow/tensorflow/issues/10085

    原文作者:风无名
    原文地址: https://www.jianshu.com/p/a3f1bfc90df3
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞