首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tf.matmul在cpu上运行的原因

tf.matmul在cpu上运行的原因
EN

Stack Overflow用户
提问于 2021-12-14 09:57:30
回答 1查看 104关注 0票数 1

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

这让我非常困惑,我试图使用tf.device('/gpu:0')来执行运行在GPU上的节点,我也试图降低tensorflow版本,但这些工作都没有。下面是我的代码:

代码语言:javascript
复制
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。

EN

回答 1

Stack Overflow用户

发布于 2022-04-02 12:03:58

可能的原因可能是GPU没有在您的系统中正确配置。这就是为什么它只检测CPU。确保您的系统中安装了Tensorflow GPU以及其他GPU支持要求。

如果TensorFlow操作同时具有CPU和GPU实现,默认情况下,在分配操作时,GPU设备将被排序。

我尝试通过选择"CPU模式“在tf.matmul()中复制Google colab,但是它无法检测到GPU:

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
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。

输出:

代码语言:javascript
复制
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)

请查看链接以获得更多详细信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70346941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档