首页 > 解决方案 > 如何检查 GPU 是否存在而不在 tensorflow 中生成警告

问题描述

所以我的问题是:

如何以简单明了的方式检查是否有 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'

标签: pythonpython-3.xtensorflowgpu

解决方案


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

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

推荐阅读