除了使用sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))其他答案以及TensorFlow官方文档中概述的方法外,您还可以尝试将计算分配给gpu,看看是否有错误。
import tensorflow as tf
with tf.device('/gpu:0'):
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)
with tf.Session() as sess:
print (sess.run(c))
这里
“ / cpu:0”:您计算机的CPU。
“ / gpu:0”:您的计算机的GPU(如果有)。
如果您有GPU并可以使用它,则将看到结果。否则,您将看到堆栈跟踪较长的错误。最后,您将获得以下内容:
无法将设备分配给节点“ MatMul”:无法满足显式设备规范“ / device:GPU:0”,因为在此过程中未注册与该规范匹配的设备
最近,一些有用的功能出现在TF中:
tf.test.is_gpu_available告知gpu是否可用
tf.test.gpu_device_name返回gpu设备的名称
您还可以在会话中检查可用设备:
with tf.Session() as sess:
devices = sess.list_devices()
devices 会给你类似的东西
[_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:CPU:0, CPU, -1, 4670268618893924978),
_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:XLA_CPU:0, XLA_CPU, 17179869184, 6127825144471676437),
_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:XLA_GPU:0, XLA_GPU, 17179869184, 16148453971365832732),
_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:0, TPU, 17179869184, 10003582050679337480),
_DeviceAttributes(/job:tpu_worker/replica:0/task:0/device:TPU:1, TPU, 17179869184, 5678397037036584928)