参考:
https://blog.csdn.net/castle_cc/article/details/78389082
import tensorflow as tf
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
查看日志信息若包含gpu信息,就是使用了gpu。
执行方式1:在 python-IDLE 中打开文件demo.py 并 Run Model :
>>>
== RESTART: D:\下载\forGPU--CUDA--TensorFlow--CUDNN\tensorflow-gpu_demo_01.py ==
>>>
什么情况?没有 gpu 信息?
执行方式2:在cmd下执行demo.py :
D:\下载\forGPU--CUDA--TensorFlow--CUDNN>tensorflow-gpu_demo_01.py
2018-11-02 17:25:34.590159: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Device mapping: no known devices.
2018-11-02 17:25:34.609960: I tensorflow/core/common_runtime/direct_session.cc:291] Device mapping:
win10 下安装 tensorflow_gpu 的各种尝试之后,终于可获取 GPU 设备了。
遇到的坑,备忘如下:
1. 为GPU显卡安装驱动: Nvidia GPU drivers: for GeForce 730
特别说明:考虑到驱动版本比CUDA9所带版本要高,可能会有冲突,可以跳过这一步,直接在第3步安装CUDA 9 时一起安装,保持版本一致,安心些。
2. 安装 Microsoft Visual C++ 2015 Redistributable Update 3 and Build Tools
说明:win10下之前安装了vs2017,但查资料要求vc++2015 Redistributable...多次安装 tensorflow_gpu 无果后,老实安装单个组件!否则CUDA9安装时就会报一个失败信息:
3. 安装 CUDA Toolkit: 9.0.176
开始安装了CUDA 10,尽管有资料显示,目前 CUDA 10 也是可以构建 tensorflow_gpu 的,但是,各种尝试失败,放弃了,老实安装 CUDA 9。
4. CUDNN: 7.0.5-win10 for CUDA 9.0
开始安装了 cuDNN 7.2 for CUDA10,后来安装了cuDNN 7.2 for CUDA 9,失败。前人的经验贴,还是要 cuDNN 7.0 才能与 CUDA 9 匹配。
5. Python 3.6.7
这个没啥坑。
6. Tensorflow_gpu : 1.10.0 for win10
开始安装的 tensorflow_gpu==1.11.0 就是找不到系统的GPU设备。嗯。
win10 环境变量设置:
所装软件:
python3 所装模块:
D:\下载\forGPU--CUDA--TensorFlow--CUDNN\005-tensorflow>pip3 list
Package Version
------------------- -------
absl-py 0.6.1
astor 0.7.1
cycler 0.10.0
gast 0.2.0
grpcio 1.16.0
h5py 2.8.0
Keras-Applications 1.0.6
Keras-Preprocessing 1.0.5
kiwisolver 1.0.1
Markdown 3.0.1
matplotlib 3.0.1
numpy 1.14.5
pip 18.1
protobuf 3.6.1
pyparsing 2.3.0
python-dateutil 2.7.5
scipy 1.1.0
setuptools 39.0.1
six 1.11.0
tensorboard 1.10.0
tensorflow-gpu 1.10.0
termcolor 1.1.0
Werkzeug 0.14.1
wheel 0.32.2
首个测试脚本:
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
测试结果:
D:\下载\forGPU--CUDA--TensorFlow--CUDNN\005-tensorflow\forPip3>python list_devices.py
2018-11-06 16:27:41.136529: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2018-11-06 16:27:41.432483: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1405] Found device 0 with properties:
name: GeForce GT 730 major: 3 minor: 5 memoryClockRate(GHz): 0.9015
pciBusID: 0000:01:00.0
totalMemory: 2.00GiB freeMemory: 1.66GiB
2018-11-06 16:27:41.442557: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1484] Adding visible gpu devices: 0
2018-11-06 16:27:42.162345: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:965] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-11-06 16:27:42.169115: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:971] 0
2018-11-06 16:27:42.174183: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:984] 0: N
2018-11-06 16:27:42.177441: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1097] Created TensorFlow device (/device:GPU:0 with 1437 MB memory) -> physical GPU (device: 0, name: GeForce GT 730, pci bus id: 0000:01:00.0, compute capability: 3.5)
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 8266507417800313319
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 1507796582
locality {
bus_id: 1
links {
}
}
incarnation: 5735230319323510955
physical_device_desc: "device: 0, name: GeForce GT 730, pci bus id: 0000:01:00.0, compute capability: 3.5"
]
最后再注明一下,其他坑花了半天时间,最后这两个坑 cuDNN 7.2 与 tensorflow_gpu==1.11.0 因为下载时看到官方说明是支持的,啊啊啊~~~ 害我折腾了两天,最后放弃抗争,采用 cuDNN 7.0 与 tensorflow_gpu==1.10.0,才搞定了。
终于搞定,开心下!