导入 Keras 时隐藏警告和/或错误

问题描述 投票:0回答:1

我的脚本导入以下 Keras 模块:

from keras.models import Sequential
from keras.layers import Dense, Input
from keras.utils import to_categorical

每次都会显示相同的警告/错误:

2024-07-20 10:51:48.653282: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-07-20 10:51:48.657088: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-07-20 10:51:48.670352: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-07-20 10:51:48.710318: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-07-20 10:51:48.722894: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-07-20 10:51:50.112114: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT

我尝试了一些我发现的建议,例如:

from keras.config import disable_interactive_logging
disable_interactive_logging()

import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)

但都不起作用。这些消息可以以某种方式隐藏吗?

python tensorflow keras cuda
1个回答
0
投票

尝试使用警告库,你可以确保TensorFlow日志消息配置为错误级别,我认为它可以帮助你

您的案例的示例代码:

import warnings
import tensorflow as tf
import os

warnings.filterwarnings('ignore', category=FutureWarning)
warnings.filterwarnings('ignore', category=UserWarning)

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
tf.get_logger().setLevel('ERROR')

os.environ['XLA_FLAGS'] = '--xla_hlo_profile'
© www.soinside.com 2019 - 2024. All rights reserved.