tensorflow中日志消息----tf.logging

1. tensorflow日志简介

TensorFlow使用五个不同级别的日志消息。 按照上升的顺序,它们是DEBUGINFOWARNERRORFATAL。 当您在任何这些级别配置日志记录时,TensorFlow将输出与该级别相对应的所有日志消息以及所有大于该级别日志。 例如,如果设置了ERROR的日志记录级别,则会收到包含ERROR和FATAL消息的日志输出,如果设置了一个DEBUG级别,则会从所有五个级别获取日志消息。默认情况下,Tensorflow在WARN的日志记录级别进行配置,但是在跟踪模型训练时,您需要将级别调整为INFO,这将提供适合操作正在进行的其他反馈。

2. tf.logging.set_verbosity()

用于设置将记录哪些日志消息的阈值.相当于python日志中的level参数

# 设置日志级别,此时显示大于等于INFO级别的日志
tf.logging.set_verbosity(tf.logging.INFO)

3. 记录日志

tf.logging.info("hellow,word")              # 记录一条info级别的日志
tf.logging.debug("hellow,word")
tf.logging.warn("helllow, word")
tf.logging.error("hellow, word")
tf.logging.fatal("hellow, word")

4. 示例

4.1 默认情况默认情况下,Tensorflow在WARN的日志记录级别进行配置

import tensorflow as tf
tf.logging.info("hellow word, info level")
tf.logging.debug("hellow word, debug level")
tf.logging.warn("hellow word, warn level")
tf.logging.error("hellow word, error level")
tf.logging.fatal("hellow word, fatal level")

输出

W0208 12:12:14.905251 90696 test.py:5] hellow word, warn level
E0208 12:12:14.905251 90696 test.py:6] hellow word, error level
E0208 12:12:14.906248 90696 test.py:7] CRITICAL - hellow word, fatal level
[Finished in 49.2s]

4.2 设置记录日志的级别

输出大于等于info级别的日志

import tensorflow as tf
tf.logging.set_verbosity(tf.logging.INFO)
tf.logging.info("hellow word, info level")
tf.logging.debug("hellow word, debug level")
tf.logging.warn("hellow word, warn level")
tf.logging.error("hellow word, error level")
tf.logging.fatal("hellow word, fatal level")

输出

I0208 12:15:57.261396 69356 test.py:3] hellow word, info level
W0208 12:15:57.261396 69356 test.py:5] hellow word, warn level
E0208 12:15:57.261396 69356 test.py:6] hellow word, error level
E0208 12:15:57.261396 69356 test.py:7] CRITICAL - hellow word, fatal level

5.其他事项

5.1 tf.logging.info(msg, *args, **args)

参数args是配合msg中的占位符用的. 比如

tf.logging.info("now %d epochs on %s ", 100, "test.py")

输出

I0208 12:36:35.534926 86200 test.py:3] now 100 epochs on test.py 
发布了33 篇原创文章 · 获赞 1 · 访问量 2608

猜你喜欢

转载自blog.csdn.net/orangerfun/article/details/104220977