Tensorflow(GPU) 在Win10+Cuda8.0环境下安装以及Cudnn包配置 图文详细教程

安装环境

  • Win10
  • Python3.6.4
    3.5以上版本都可以,目前Tensorflow只支持64位python3.5以上版本
  • numpy
    安装好Python后打开终端cmd输入 pip3 install numpy

具体流程

  1. 下载安装Cuda8.0一定要是8.0版本!下载地址,并按照下图选择下载本地安装包。

    image

    如果安装错了记得要把之前的删除卸载干净

  2. 安装完成后配置系统环境变量Path

    TensorFlow 是一个编程系统, 使用图来表示计算任务,图必须在Session(会话)里被启动. Session将图的op(操作)分发到诸如CPU或GPU之类的设备上运行。所以,这个时候你运行python然后import tensorflow as tf是不会报错的,但是当你要执行tf.Session()的时候可能就有问题了。这个时候将会调用cuda,我在这里遇到的问题是各种lib,dll加载不了。经过一番检查,定位到问题,Cuda安装完成后默认的环境变量配置不对,不能直接访问到binlib\x64下的程序包,在path中加上这两个路径即可。

    image

    原本安装好之后并不会有以上四个环境变量,有两个需要自己加上。

    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

    附配置环境变量教程
    最后在cmd里输入 echo %path% 就能查看你的是否添加进环境变量了

  3. 下载Cudnn6.0,下载地址,需要注册并填问卷,下载后解压压缩包,将包内文件夹里面的内容分别拷贝到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0里面的三个文件夹中去。

    image

  4. 最后测试和检查一下,代码如下

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

如果失败的话记得检查一下报错信息,没有安装CUDA8.0或者环境配置不对:

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

安装成功:

TensorFlow successfully installed.
The installed version of TensorFlow includes GPU support.

注意几点

  1. Cuba一定要安装8.0版本!Cuba一定要安装8.0版本!Cuba一定要安装8.0版本!
  2. Anaconda并不是必需,可以使用可以不使用
  3. Cudnn的版本我这里提示的是Cudnn6,大家看提示安装

后续

跑个DQN玩FlappyBird测试:


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

推荐阅读更多精彩内容