我在训练我的模型时遇到了一些问题,特别是,训练速度有时快,有时慢(在每秒2-10层之间波动)。在使用张量板检查网络操作后,我发现tf.matmul()节点在CPU设备上运行。详情如下:

这让我非常困惑,我试图使用tf.device('/gpu:0')来执行运行在GPU上的节点,我也试图降低tensorflow版本,但这些工作都没有。下面是我的代码:
def _repeat(x, n_repeats):
with tf.variable_scope('_repeat'):
rep = tf.transpose(
tf.expand_dims(tf.ones(shape=tf_v.stack([n_repeats, ])), 1), [1, 0])
rep = tf.cast(rep, tf.int32)
# with tf.device('/gpu:0'):
x = tf.matmul(tf.reshape(x, (-1, 1)), rep)
return tf.reshape(x, [-1])顺便说一下,tensorflow版本是2.2.5,GPU是rtx3090。
发布于 2022-04-02 12:03:58
可能的原因可能是GPU没有在您的系统中正确配置。这就是为什么它只检测CPU。确保您的系统中安装了Tensorflow GPU以及其他GPU支持要求。
如果TensorFlow操作同时具有CPU和GPU实现,默认情况下,在分配操作时,GPU设备将被排序。
我尝试通过选择"CPU模式“在tf.matmul()中复制Google colab,但是它无法检测到GPU:
import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
print(tf.config.list_physical_devices())
# Place tensors on the GPU
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]])
c = tf.matmul(a, b)
print(c)输出:
Num GPUs Available: 0
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]
tf.Tensor(
[[22. 28.]
[49. 64.]], shape=(2, 2), dtype=float32)但如果通过选择"GPU模式“进行同样的尝试,张量操作就会检测到GPU。
输出:
Num GPUs Available: 1
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'), PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
tf.Tensor(
[[22. 28.]
[49. 64.]], shape=(2, 2), dtype=float32)请查看这链接以获得更多详细信息。
https://stackoverflow.com/questions/70346941
复制相似问题