在tensorflow中实现屏蔽输出的log信息

 更新时间:2020年02月04日 08:39:29   作者:-牧野-  
今天小编就为大家分享一篇在tensorflow中实现屏蔽输出的log信息,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

tensorflow中可以通过配置环境变量 'TF_CPP_MIN_LOG_LEVEL' 的值,控制tensorflow是否屏蔽通知信息、警告、报错等输出信息。

使用方法:

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 : 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也是默认的输出,分为三部分,一个是警告信息说没有优化加速,二是通知信息告知操作所用的设备,三是程序中代码指定要输出的结果信息

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中bytes和str的区别与联系详解

    Python中bytes和str的区别与联系详解

    Python3最重要的新特性之一是对字符串和二进制数据流做了明确的区,下面这篇文章主要给大家介绍了关于Python中bytes和str区别与联系的相关资料,需要的朋友可以参考下
    2022-05-05
  • python数据类型的详细分析(附示例代码)

    python数据类型的详细分析(附示例代码)

    这篇文章主要给大家介绍了关于python数据类型分析的相关资料,python里可以通过type()函数来查看数据类型,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2021-09-09
  • python按列索引提取文件夹内所有excel指定列汇总(示例代码)

    python按列索引提取文件夹内所有excel指定列汇总(示例代码)

    这篇文章主要介绍了python按列索引提取文件夹内所有excel指定列汇总,本文通过多种场景分析结合示例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-03-03
  • Python执行流程控制详情

    Python执行流程控制详情

    这篇文章主要介绍了Python执行流程控制,流程控制即控制流程,具体指控制程序的执行流程,而程序的执行流程分为三种结构:顺序结构、分支结构、循环结构,下文详细介绍需要的小伙伴可以参考一下
    2022-04-04
  • Python脚本/代码的几种常见运行方式

    Python脚本/代码的几种常见运行方式

    我们知道,python脚本或者说python程序其实是一个包含了python代码的文件,通过运行python代码,我们可以验证脚本/程序是否按照我们的期望执行,在python中,有多种方式来运行脚本或程序,取决于小伙伴们的需求,接下来小编将介绍几种常见的 python 代码运行方式
    2023-10-10
  • python3.6环境下安装freetype库和基本使用方法(推荐)

    python3.6环境下安装freetype库和基本使用方法(推荐)

    这篇文章主要介绍了python3.6环境下如何安装freetype库和基本使用方法,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-05-05
  • python写完程序怎么运行的两种方式

    python写完程序怎么运行的两种方式

    这篇文章主要介绍了Python的两种运行方式,分别是命令行和交互式命令行,并详细讲解了如何在命令行和交互式命令行中运行Python代码,需要的朋友可以参考下
    2025-03-03
  • Django中使用Redis配置缓存的方法步骤

    Django中使用Redis配置缓存的方法步骤

    本文主要介绍了Django中使用Redis配置缓存的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-05-05
  • Python DataFrame实现固定周期内统计每列的非零值

    Python DataFrame实现固定周期内统计每列的非零值

    在数据处理中,使用DataFrame统计固定周期内每列的非零值数量是一种常见需求,通过将数据分组并使用计数函数,可以方便地实现此目标,具体方法包括首先计算每列的0值个数,然后通过总数减去0值个数得到非零值的数量
    2024-09-09
  • PyTorch手写数字数据集进行多分类

    PyTorch手写数字数据集进行多分类

    这篇文章主要介绍了PyTorch手写数字数据集进行多分类,损失函数采用交叉熵,激活函数采用ReLU,优化器采用带有动量的mini-batchSGD算法,需要的朋友可以参考一下
    2022-03-03

最新评论