如何更改 Tensorflow 2 中的日志级别

lea*_*ine 6 tensorflow

到目前为止我已经尝试过

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

import logging
import tensorflow as tf
logger = tf.get_logger()
logger.setLevel(logging.ERROR)

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
Run Code Online (Sandbox Code Playgroud)

这些似乎都不起作用。

小智 3

事实上,tf.compat.v1.logging.set_verbosity有效。

请找到下面的代码,其中Logging Level设置为10 i.e., DEBUG,含义仅all the Logs应该是Printed

import logging
import tensorflow as tf

tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.DEBUG)

tf.compat.v1.logging.error('Error Message')
tf.compat.v1.logging.info('Info Message')
tf.compat.v1.logging.warning('Warning Message')
Run Code Online (Sandbox Code Playgroud)

如下图所示,已经打印了所有的Log Message:

ERROR:tensorflow:Error Message
INFO:tensorflow:Info Message
WARNING:tensorflow:Warning Message
Run Code Online (Sandbox Code Playgroud)

现在,我们将其设置Logging Level30, i.e., WARN

import logging
import tensorflow as tf

tf.compat.v1.logging.set_verbosity(30) # WARN

tf.compat.v1.logging.error('Error Message')
tf.compat.v1.logging.info('Info Message')
tf.compat.v1.logging.warning('Warning Message')
Run Code Online (Sandbox Code Playgroud)

如下图,InfoLogs将被过滤并Warning and Error Logs打印:

ERROR:tensorflow:Error Message
WARNING:tensorflow:Warning Message
Run Code Online (Sandbox Code Playgroud)

让我们将其设置Logging Level40 i.e., ERROR

import logging
import tensorflow as tf

tf.compat.v1.logging.set_verbosity(40) # ERROR

tf.compat.v1.logging.error('Error Message')
tf.compat.v1.logging.info('Info Message')
tf.compat.v1.logging.warning('Warning Message')
Run Code Online (Sandbox Code Playgroud)

现在,我们可以看到只Error Message打印了:

ERROR:tensorflow:Error Message
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。快乐学习!