零基础深度学习笔记3——Win7-Tensorflow-GPU安装

之前已经在另一台电脑上安装好了CPU版本的TF,

以为GPU版本的步骤什么的应该也不难,没想到还是有些坑要填。


清单:

系统:WIN7

python:3.5版本

CUDA:8.0

Cudnn:v6.0(这里版本的选择很重要!!!)

Tensorflow:1.3


下面说下安装过程:

默认WIN7系统已经装好了

一、下载安装python :https://www.python.org/downloads/release/python-352/


下载完直接双击安装就好了


二、安装cuda8.0:

1.      下载cuda8.:https://developer.nvidia.com/cuda-downloads 下载cuda_8.0.61_windows.exe

2.      安装显卡驱动:从http://www.geforce.cn/drivers下载匹配的驱动,我用的是GTX1080;

3.      安装cuda8.0:双击cuda_8.0.61_windows.exe直接进行安装即可,默认安装到C:\ProgramFiles\NVIDIA GPU Computing Toolkit目录下;

4.      验证cuda8.0已正确安装:

打开cmd,输入$ nvcc  -V,结果如下图:


三、下载cudnn 6.0:

https://developer.nvidia.com/rdp/cudnn-download

解压后分别将cuda/include、cuda/lib、cuda/bin三个目录中的内容拷贝到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0对应的include、lib、bin目录下即可。

这里一定要注意cudnn的版本要和自己的Tensorflow版本相匹配,否则会出现  “DLL load failed: 找不到指定的模块等各种报错。

我原先下载的是7.0版本的,后面又查资料说要有cuDNN64_5.dll.就又下载了5.0的,但还是无法运行,后面幸亏找到了个筛查错误的程序,才发现要6.0版本的。如果你安装完TF后,发现不能正常运行,可以试下这个筛查代码,如果都OK,请跳过

代码如下:

# 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.")

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

新建一个 tensorflow_self_check.py,然后用python运行就好了

我得到的结果如下,注意圈红色的部分(TF1.2.1或者更早版本的要cudnn64_5.dll,也就是v5版本,但1.3及之后的要cudnn64_6.dll,也就是v6版本):



四、安装Tensorflow

官网上有两种安装方法,一种是直接安装,另一种是用Anaconda安装,我用的是第一种

如果是只有CPU输入:pip3 install --upgrade tensorflow

     如果是有GPU的输入:pip3 install --upgrade tensorflow-gpu

五、验证是否已正确安装TF

输入

$ python
激活python环境,然后输入

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
如果能够显示
Hello, TensorFlow!
那就说明安装成功啦!~~


参考网站:

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

http://blog.csdn.net/fengbingchun/article/details/53892997

https://gist.github.com/mrry/ee5dbcfdd045fa48a27d56664411d41c#file-tensorflow_self_check-py


你可能感兴趣的:(深度学习血泪史)