我想检查gpu是否在tensorflow版本1中使用



gpu仍低于5%,是否有任何代码可以检查模型是否使用gpu?(Tensorflow 1.15(

使用下面的代码片段告诉Tensorflow使用GPU

import tensorflow as tf 
if tf.test.gpu_device_name():
print('Default GPU Device:{}'.format(tf.test.gpu_device_name()))
else:
print("Please install GPU version of TF")

输出

Default GPU Device:/device:GPU:0

如果您希望在您选择的设备上运行特定的操作,而不是自动为您选择的操作,则可以与tf.device一起使用来创建设备上下文,并且该上下文中的所有操作都将在同一指定设备上运行。

tf.debugging.set_log_device_placement(True)
# Place tensors on the CPU
with tf.device('/GPU:0'):
a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
# Run on the GPU
c = tf.matmul(a, b)
print(c)

最新更新