python - 如何检查 GPU 是否存在而不在 tensorflow 中生成警告
问题描述
我正在编写代码来训练模型,我不确定这段代码是否会在以后有或没有 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
您的警告告诉您使用的 from tf.config.experimental 。这段代码对我有用。
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
print('GPU is available')
推荐阅读
- flutter - 在 Flutter 中设置背景图片的高度、宽度、位置
- html - 我怎样才能把这 4 张图片放在 1 张图片中?
- reactjs - React - 将 props 转移到已经是 React 元素的变量
- mysql - 匹配包含多个单词的正则表达式
- python - How to find the mathematical expression for finding a missing number in an array
- c# - 每 0.5f 切换一次布尔值?
- ios - CNContactPickerViewController 仅检索所选项目
- javascript - 反应路由器不改变显示的组件
- mysql - NodeJS 无法连接到 Docker 容器内的 MYSQL 最新版本
- python - 带有 KeyError 的 pandas 简单循环:8167