python - 如何在 tensorflow 中检查 GPU 是否存在而不生成警告

标签 python python-3.x tensorflow gpu

  • 我正在编写代码来训练模型,我不确定该代码稍后是否会在有或没有 GPU 的机器上运行,因此我正在使用该代码。

  • 我的问题是:代码生成此警告:

    • is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version. Instructions for updating: Use tf.config.list_physical_devices('GPU') instead.

所以我的问题是:

如何以简单明了的方式检查是否有 GPU,而不产生警告。

import os

# Check if there is valid GPU to be used
if(tf.test.is_gpu_available(cuda_only=True)):
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'

最佳答案

尝试使用list_physical_devices来自 tf.config.experimental 您的警告告诉您要使用。这段代码对我有用。

gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
  print('GPU is available')

关于python - 如何在 tensorflow 中检查 GPU 是否存在而不生成警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61991694/

相关文章:

python - 如何在 python 中扩展字符串中的字符串?

python - 如何将 Pandas 数据框中的 UTM 转换为经度/纬度?

python-3.x - 如何找到output_node_name来构建卡住图?

python - 在pytorch中沿行散布张量

python - 在 Spark ML 中创建自定义交叉验证

python - __new__ 在正确的类时不调用 __init__

python - lambda 参数解包错误

python - 在 Windows 中使用 Python 3 连接到 SQL Server

tensorflow - fit() 按预期工作,但随后在 evaluate() 模型中随机执行

c++ - 在 tensorflow c++ 中是否有等同于 tf.convert_to_tensor 的东西?