簡體   English   中英

如何在 Tensorflow 2 中更改日志級別

[英]How to change log level in Tensorflow 2

到目前為止我已經嘗試過

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'

這些似乎都不起作用。

實際上, tf.compat.v1.logging.set_verbosity有效。

請在下面的代碼中找到Logging Level設置為10 ie, DEBUG的代碼, 10 ie, DEBUG ,這意味着應該只Printed all the Logs

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')

如下圖,它已經打印了所有的日志消息:

ERROR:tensorflow:Error Message
INFO:tensorflow:Info Message
WARNING:tensorflow:Warning Message

現在,讓我們將Logging Level設置為30, ie, 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')

如下所示, Info日志將被過濾並打印Warning and Error Logs

ERROR:tensorflow:Error Message
WARNING:tensorflow:Warning Message

讓我們將Logging Level設置為40 ie, 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')

現在,我們可以看到只打印了Error Message

ERROR:tensorflow:Error Message

希望這可以幫助。 快樂學習!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM