[英]Why does tf.matmul(a,b, transpose_b=True) work, but not tf.matmul(a, tf.transpose(b))?
[英]Why tf.matmul running on cpu
我在训练我的model时遇到了一些问题,具体来说,训练速度有时快有时慢(在每秒2-10次迭代之间波动)。 我用tensorboard查看.network运行后,发现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。
可能的原因可能是您的系统中没有正确配置 GPU。 这就是它只检测 CPU 的原因。 确保您的系统中安装了Tensorflow GPU
以及其他GPU 支持要求。
如果 TensorFlow 操作同时具有 CPU 和 GPU 实现,默认情况下,分配操作时优先考虑 GPU 设备。
我尝试通过选择“CPU 模式”在Google colab
中复制tf.matmul()
,但它无法检测到 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)
Output:
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。
Output:
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)
请检查此链接以获取更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.