环境:windows7-64位+NVIDIA GeForce GTX 750 Ti
配置:CUDA 9.1、cuDNN v7.1.3、tensorflow-gpu-1.12.0、python 3.6.3
(整理一下做个记录)
一、tensorflow CUDA cudnn 版本对应关系
(查看https://tensorflow.google.cn/install/source)
Linux:
windows:
二、安装
1.查看并安装显卡支持的cuda版本
可以看到,该显卡支持的cuda版本是CUDA9.1,因此去
INVIDIA-CUDA官网: https://developer.nvidia.com/cuda-toolkit-archive下载对应版本的CUDA。
再由上面的tensorflow CUDA cudnn 版本对应关系,查找到cuDNN的版本为7、tensorflow-gpu-1.12.0、python 3.6.3。
INVIDA-cuDNN官网:https://developer.nvidia.com/rdp/cudnn-archive 下载对应版本的cuDNN。
z
cuda下载:
cuDNN下载:
1. CUDA安装:直接运行.exe文件即进行安装,完成后验证CUDA是否安装成功:cmd: nvcc -V
、
2. cuDNN安装:
(1)解压缩下载的CuDnn文件,得到3个文件夹:bin, include, lib。将这个三个文件夹复制到“C:\ProgramData\NVIDIA GPU Computing Toolkit\v9.1”。
(2)环境变量设置
确认CUDA_PATH和CUDA_PATH_V8.0已经存在;
手动添加C:\ProgramData\NVIDIA GPU Computing Toolkit\v9.1\bin到%Path%中
3. Tensorflow的GPU版本安装:
cmd输入:pip install tensorflow-gpu 会自动安装最新版,若要指定版本: pip install tensorflow-gpu==1.12.0
4. 测试TensorFlow
通过以上步骤已经完成了安装工作,下面可以通过一个简单的程序测试TensorFlow是否安装成功。
- 直接在CMD中键入以下代码:
import tensorflow as tf
sess = tf.Session()
a = tf.constant(1)
b = tf.constant(2)
print(sess.run(a+b))
结果: 3
即为成功安装!
tips: 若报错:ImportError: Could not find 'cudart64_100.dll'
问题是找不到cuda系的dll文件的模块,提示需要下载CUDA10.0,那么首先查看cuda的路径下是否存在该文件:
通过C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA路径访问cuda,在其bin目录下查找是否有cudart64_100.dll模块
如果有,则查看环境变量是否添加;如果没有,可能就是cuda版本和tensorflow版本的匹配问题。https://www.tensorflow.org/install/gpu#software_requirements
由于开始使用pip install tensorflow-gpu 自动安装了最新版(1.14.0)需要cuda10.0的支持,故降低版本: pip install tensorflow-gpu==1.12.0,问题解决。
- 或在Pycharm中选用这个tensorflow的编译器,测试代码:
import tensorflow as tf
# Creates a graph.
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)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))
结果:成功!
D:\apps\PyCharm 2019.1.3\helpers\pydev\_pydev_bundle\pydev_import_hook.py:21: 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`.
module = self._system_import(name, *args, **kwargs)
2019-07-21 10:21:46.465870: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2019-07-21 10:21:46.672882: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties:
name: GeForce GTX 750 Ti major: 5 minor: 0 memoryClockRate(GHz): 1.2285
pciBusID: 0000:01:00.0
totalMemory: 2.00GiB freeMemory: 177.84MiB
2019-07-21 10:21:46.672882: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0
2019-07-21 10:21:47.545932: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-07-21 10:21:47.545932: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988] 0
2019-07-21 10:21:47.545932: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0: N
2019-07-21 10:21:47.545932: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 113 MB memory) -> physical GPU (device: 0, name: GeForce GTX 750 Ti, 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 750 Ti, pci bus id: 0000:01:00.0, compute capability: 5.0
2019-07-21 10:21:47.547932: 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 750 Ti, pci bus id: 0000:01:00.0, compute capability: 5.0
MatMul: (MatMul): /job:localhost/replica:0/task:0/device:GPU:0
2019-07-21 10:21:47.549932: 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
b: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2019-07-21 10:21:47.549932: I tensorflow/core/common_runtime/placer.cc:927] a: (Const)/job:localhost/replica:0/task:0/device:GPU:0
2019-07-21 10:21:47.549932: I tensorflow/core/common_runtime/placer.cc:927] b: (Const)/job:localhost/replica:0/task:0/device:GPU:0
[[22. 28.]
[49. 64.]]
- 使用nvidia-smi查看GPU使用情况:(注意在nvidia-smi.exe所在目录下运行)