TensorFlow 中通过环境变量屏蔽日志输出

七月
七月
发布时间:2026-10-13 | 5人浏览过
原创

本文介绍如何在 TensorFlow 中通过设置环境变量 TF_CPP_MIN_LOG_LEVEL 来屏蔽不同级别的日志输出。该参数支持 0 至 3 四个值,分别对应输出所有信息、屏蔽通知、屏蔽通知与警告、以及屏蔽所有非错误信息。通过具体代码示例展示了各取值下的实际输出效果,帮助开发者优化控制台显示。

TensorFlow 日志控制机制

在 TensorFlow 框架中,用户可以通过配置特定的环境变量 'TF_CPP_MIN_LOG_LEVEL' 来控制程序运行时输出的日志级别,从而实现对通知信息、警告信息以及报错信息的屏蔽或显示控制。

TF_CPP_MIN_LOG_LEVEL 参数详解

具体的配置方法如下:


import os
import tensorflow as tf
 
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # or any {'0', '1', '2'}

当 TF_CPP_MIN_LOG_LEVEL 设置为 0 时(这也是默认值),系统将输出所有的日志信息,包括通知、警告和错误。

当 TF_CPP_MIN_LOG_LEVEL 设置为 1 时,系统将屏蔽所有通知级别的日志信息。

当 TF_CPP_MIN_LOG_LEVEL 设置为 2 时,系统将屏蔽通知信息和警告信息。

当 TF_CPP_MIN_LOG_LEVEL 设置为 3 时,系统将屏蔽通知信息、警告信息以及报错信息,仅保留极少量的关键错误。

测试代码与输出效果分析

以下是用于测试上述配置效果的代码示例:


import tensorflow as tf
import os
 
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '0'
# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
 
v1 = tf.constant([1.0, 2.0, 3.0], shape=[3], name='v1')
v2 = tf.constant([1.0, 2.0, 3.0], shape=[3], name='v2')
sumV12 = v1 + v2
 
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
 print sess.run(sumV12)

当 TF_CPP_MIN_LOG_LEVEL 设置为 0 时的典型输出结果如下:


2018-04-21 14:59:09.910415: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2018-04-21 14:59:09.910442: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2018-04-21 14:59:09.910448: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2018-04-21 14:59:09.910453: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2018-04-21 14:59:09.910457: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2018-04-21 14:59:09.911260: I tensorflow/core/common_runtime/direct_session.cc:300] Device mapping:
2018-04-21 14:59:09.911816: I tensorflow/core/common_runtime/simple_placer.cc:872] add: (Add)/job:localhost/replica:0/task:0/cpu:0
2018-04-21 14:59:09.911835: I tensorflow/core/common_runtime/simple_placer.cc:872] v2: (Const)/job:localhost/replica:0/task:0/cpu:0
2018-04-21 14:59:09.911841: I tensorflow/core/common_runtime/simple_placer.cc:872] v1: (Const)/job:localhost/replica:0/task:0/cpu:0
Device mapping: no known devices.
add: (Add): /job:localhost/replica:0/task:0/cpu:0
v2: (Const): /job:localhost/replica:0/task:0/cpu:0
v1: (Const): /job:localhost/replica:0/task:0/cpu:0
[ 2. 4. 6.]

值为 0 或默认状态下的输出通常包含三部分主要内容:首先是关于未启用硬件加速(如 SSE4.1、AVX 等指令集)的警告信息;其次是告知当前操作所用设备的通知信息;最后是程序中代码明确指定输出的计算结果。


TF_CPP_MIN_LOG_LEVEL 为 1 的输出,没有通知信息了:

2018-04-21 14:59:09.910415: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2018-04-21 14:59:09.910442: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2018-04-21 14:59:09.910448: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2018-04-21 14:59:09.910453: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2018-04-21 14:59:09.910457: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
Device mapping: no known devices.
add: (Add): /job:localhost/replica:0/task:0/cpu:0
v2: (Const): /job:localhost/replica:0/task:0/cpu:0
v1: (Const): /job:localhost/replica:0/task:0/cpu:0
[ 2. 4. 6.]

当 TF_CPP_MIN_LOG_LEVEL 设置为 2 或 3 时的输出对比显示,若设置为 2 则不再显示警告信息;若设置为 3,则连警告信息和潜在的报错信息(如果存在)也会被完全屏蔽。


Device mapping: no known devices.
add: (Add): /job:localhost/replica:0/task:0/cpu:0
v2: (Const): /job:localhost/replica:0/task:0/cpu:0
v1: (Const): /job:localhost/replica:0/task:0/cpu:0
[ 2. 4. 6.]

以上即为在 TensorFlow 中实现屏蔽输出 log 信息的完整方法,希望能为读者提供有效的参考与帮助。

相关标签:

本站声明

本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。

Python 相关产品和应用

全部下载

专题列表