天天看點

如何确定TensorFlow使用的是GPU運作

隻要配置好了GPU的驅動和cuda 以及cudnn等工具(配置好環境),程式執行會預設先使用GPU的,如果不能執行的話再使用CPU。有些配置好cuda的機器能夠執行tensorflow,但是運作慢,可能是因為運作使用的是CPU運作,而不是GPU在執行程式,如何判斷我們的程式是使用CPU還是GPU在執行TensorFlow。

可使用下面小程式測試:(參考:TensorFlow實戰Google深度學習架構;鄭澤宇)

import tensorflow as tf

a = tf.constant([1.2,2.3,3.6], shape=[3],name='a')
b = tf.constant([1.2,2.3,3.6], shape=[3],name='b')

c = a+b
session = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print(session.run(c))
           

如果是GPU運作,則輸入以下類似輸出,會顯示有GPU的輸出:

Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Quadro M5000, pci bus id: 0000:01:00.0
add: (Add): /job:localhost/replica:0/task:0/gpu:0
b: (Const): /job:localhost/replica:0/task:0/gpu:0
a: (Const): /job:localhost/replica:0/task:0/gpu:0
[2.4 4.6 7.2]
           

如果是CPU運作則輸入一下類似輸出,不論你有幾個核,都是CPU:0:

add: (Add): /job:localhost/replica:0/task:0/device:CPU:0
a: (Const): /job:localhost/replica:0/task:0/device:CPU:0
b: (Const): /job:localhost/replica:0/task:0/device:CPU:0
[2.4 4.6 7.2]
           

繼續閱讀