Tensorflow操作似乎没有使用GPU



我需要执行一项多次平均大量长向量的工作,我希望这项工作在我的GPU上完成。

在运行时监测nvtop和htop,我发现GPU(在我训练Keras模型时总是显示顶部活动(在这些操作中根本没有使用,而CPU在这些操作期间使用激增。

我在下面的代码片段中对其进行了模拟(试图尽量减少非tf工作(。

我做错了什么?

import tensorflow as tf
from tensorflow.math import add_n, add, scalar_mul
import numpy as np
tf.debugging.set_log_device_placement(True)
sess = tf.compat.v1.Session(config=config) 
tf.compat.v1.keras.backend.set_session(sess)
os.environ["CUDA_VISIBLE_DEVICES"]="1"
#Make a random numpy matrix
vecs=np.random.rand(100, 300)
with sess.as_default():
with tf.device('/GPU:0'):
for _ in range(1000):
#vecs=np.random.rand(100, 300)
tf_vecs=tf.Variable(vecs, dtype=tf.float64)
tf_invlgt=tf.Variable(1/np.shape(vecs)[0],dtype=tf.float64)
vectors=tf.unstack(tf_vecs)
sum_vecs=add_n(vectors)
mean_vec=tf.Variable(scalar_mul(tf_invlgt, sum_vecs))

感谢

Michael

我可能错了,但cuda_visible_devices应该是"0";像

import os
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="0"

请参阅此处的github评论

如果仍然不起作用,您还可以添加一小段代码来检查tensorflow是否可以看到gpu设备:

from tensorflow.python.client import device_lib
def get_available_gpus():
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == 'GPU']

这里提到了

最新更新