簡體   English   中英

為什么 tf.matmul 在 cpu 上運行

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM