【问题标题】:Tensorflow 2.0 train model on single GPUTensorFlow 2.0 在单 GPU 上训练模型
【发布时间】:2020-11-27 06:19:03
【问题描述】:

我想在单个 NVIDIA 显卡(RTX 2080 super)上训练一个顺序 tensorflow(版本 2.3.0)模型。我正在使用以下代码 sn-p 来构建和训练模型。但是,每次运行此代码时,我都看不到任何 GPU 利用率。任何建议如何修改我的代码以便我可以在 1 个 GPU 上运行它?

strategy = tf.distribute.OneDeviceStrategy(device="/GPU:0")
with strategy.scope():
    num_classes=len(pd.unique(cats.No))
    model = BuildModel((image_height, image_width, 3), num_classes)
    model.summary()
    model=train_model(model,valid_generator,train_generator,EPOCHS,BATCH_SIZE)

【问题讨论】:

  • 如果你只有 1 个 GPU,为什么要使用 tf.distribute?
  • 好吧,我只是想确保训练是在 GPU 而不是 CPU 上完成的。它不起作用,因为我可以看到我的 GPU 上没有进行任何计算
  • 你有这个问题的消息吗?我也有这个问题

标签: python tensorflow gpu nvidia


【解决方案1】:

运行以下代码以查看 tensorflow 是否检测到您的 GPU。

import tensorflow as tf
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
print(tf.__version__)
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
tf.test.is_gpu_available()
!python --version

【讨论】:

  • 它确实检测到了 gpu: Num GPUs Available: 1 WARNING:tensorflow:From :6: is_gpu_available (from tensorflow.python.framework.test_util) 已被弃用并且将在未来的版本中删除。更新说明:改用tf.config.list_physical_devices('GPU')。 Python 3.7.7