关于在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


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

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


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


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



这里我忽略了警告, 继续完成安装, 并安装了后续的补丁包


这里可以不勾选Visual Studio Integration

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

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


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


这是楼主全部的环境变量, 除了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是一个压缩包, 解压后应该是



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

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

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


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

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

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

个人更喜欢这个


使用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.



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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,240评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,328评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,182评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,121评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,135评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,093评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,013评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,854评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,295评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,513评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,678评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,398评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,989评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,636评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,801评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,657评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,558评论 2 352

推荐阅读更多精彩内容