Tensorflow-gpu和cuda安装配置

本文为原创博文,文本链接https://blog.csdn.net/weixin_44633882/article/details/86771631

所使用的环境:

  • Windows 10
  • GeForce GTX 960M
  • Anaconda3-5.2.0-Windows
  • python3.6.5
  • Tensorflow-gpu-1.12.0
  • CUDA 9.0
  • cuDNN v7.4.1 (Nov 8, 2018), for CUDA 9.0

1. python

安装Anacoda3-5.2.0

2. Tensorflow-gpu

使用pip安装tensorflow-gpu

pip install tensorflow-gpu
Collecting tensorflow-gpu
  Downloading https://files.pythonhosted.org/packages/88/73/13e4071739df8d5ee7a27780d66bc98a51612521ad7e5a1e468d9507087c/tensorflow_gpu-1.12.0-cp36-cp36m-win_amd64.whl (80.0MB)
    100% |████████████████████████████████| 80.0MB 336kB/s
...
Successfully installed tensorflow-gpu-1.12.0

安装完成

3. 使用Tensorflow

先直接调用tensorflow,会发现找不到指定模块。
接下来我们就安装CUDA和cuDNN

>>> import tensorflow
Traceback (most recent call last):
  File "C:\Users\11244\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\11244\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\11244\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "C:\Users\11244\Anaconda3\lib\imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\11244\Anaconda3\lib\imp.py", line 343, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: 找不到指定的模块。
下载CUDA和cuDNN会要求注册NVIDA账号

4. 安装CUDA9.0

首先,在https://developer.nvidia.com/cuda-gpus中查看GPU对应cuda版本,看看你的显卡是否支持

我使用GeForce GTX 960M

然后在https://developer.nvidia.com/cuda-toolkit-archive中选择CUDA9.0

安装(一直点击下一步,即可安装完成)

测试

C:\Users\11244\Desktop> nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Sep__1_21:08:32_Central_Daylight_Time_2017
Cuda compilation tools, release 9.0, V9.0.176

看到cuda,表示安装成功

参考 https://blog.csdn.net/AAlonso/article/details/81504036

5. 下载cuDNN(安装CUDA9.0对应最新版本的cuDNN)

登陆https://developer.nvidia.com/rdp/cudnn-archive
选择cuDNN v7.4.1 (Nov 8, 2018), for CUDA 9.0
选择Windows 10版本下载

解压压缩包

将解压后的三个文件夹bin、include、lib文件夹复制到安装CUDA9.0的路径覆盖原有的文件夹。本机的安装路径为:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0

6.添加环境变量

将C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin,C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\include,C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\lib\x64,这三个路径填到系统变量的Path中

到这一步,cuda和cuDNN都配置完成

7. 测试

Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
C:\Users\11244\Anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
>>> a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
>>> b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
>>> c = tf.matmul(a, b)
>>> sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
2019-02-07 14:25:02.955306: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2019-02-07 14:25:03.545403: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties:
name: GeForce GTX 960M major: 5 minor: 0 memoryClockRate(GHz): 1.0975
pciBusID: 0000:01:00.0
totalMemory: 2.00GiB freeMemory: 1.65GiB
2019-02-07 14:25:03.551711: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0
2019-02-07 14:25:04.680683: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-02-07 14:25:04.685372: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988]      0
2019-02-07 14:25:04.688090: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0:   N
2019-02-07 14:25:04.691630: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 1399 MB memory) -> physical GPU (device: 0, name: GeForce GTX 960M, pci bus id: 0000:01:00.0, compute capability: 5.0)
Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce GTX 960M, pci bus id: 0000:01:00.0, compute capability: 5.0
2019-02-07 14:25:04.702360: I tensorflow/core/common_runtime/direct_session.cc:307] Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce GTX 960M, pci bus id: 0000:01:00.0, compute capability: 5.0

>>> print(sess.run(c))
MatMul: (MatMul): /job:localhost/replica:0/task:0/device:GPU:0
2019-02-07 14:25:11.427809: I tensorflow/core/common_runtime/placer.cc:927] MatMul: (MatMul)/job:localhost/replica:0/task:0/device:GPU:0
a: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2019-02-07 14:25:11.431749: I tensorflow/core/common_runtime/placer.cc:927] a: (Const)/job:localhost/replica:0/task:0/device:GPU:0
b: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2019-02-07 14:25:11.435717: I tensorflow/core/common_runtime/placer.cc:927] b: (Const)/job:localhost/replica:0/task:0/device:GPU:0
[[22. 28.]
 [49. 64.]]

配置完成

参考博客

  1. https://www.cnblogs.com/gaofighting/p/9917456.html
  2. https://blog.csdn.net/freewebsys/article/details/81276120
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐