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

yizhihongxing

在TensorFlow中我们可以使用日志信息(log)来记录和追踪代码运行过程中的各种信息,这对于调试和优化代码非常有用。但由于TensorFlow输出大量信息的log,可能会造成输出信息混乱的问题。因此,本文将介绍如何实现屏蔽TensorFlow输出的log信息。

方法一:利用Python的日志模块

第一种方法是使用Python标准库中的logging模块来控制TensorFlow的日志信息输出。

在TensorFlow中,我们可以使用如下代码来实现日志输出:

import tensorflow as tf
tf.logging.set_verbosity(tf.logging.INFO)

其中,tf.logging.set_verbosity()函数用来控制TensorFlow输出的日志信息等级,常见的等级包括INFO、DEBUG和ERROR等。但是,我们可以通过调用Python的logging模块来控制TensorFlow输出日志信息等级,轻松实现屏蔽TensorFlow输出的log信息。

具体操作步骤如下:

  1. 导入logging模块
import logging
  1. 设置logging模块的日志等级
logging.getLogger('tensorflow').setLevel(logging.ERROR)

其中,'tensorflow'是TensorFlow默认使用的模块名称,通过设置日志等级可以屏蔽一定等级以下的日志信息输出,例如,上述设置将屏蔽INFO、DEBUG等级的日志信息输出。

  1. 查看日志信息
tf.logging.info("This is a info message")
tf.logging.error("This is a error message")

通过以上设置,我们可以轻松控制TensorFlow输出日志信息的等级,实现屏蔽TensorFlow输出的log信息。

方法二:利用TensorFlow的日志记录器

第二种方法是利用TensorFlow的日志记录器(tf.logging.Logger)实现屏蔽TensorFlow输出的log信息。与方法一不同的是,这种方法并不需要依赖Python的logging模块。

具体操作步骤如下:

  1. 创建日志记录器
import tensorflow as tf
logger = tf.logging.Logger("my_logger")

其中,"my_logger"是我们自定义的日志记录器名称。

  1. 设置日志等级
logger.setLevel(tf.logging.ERROR)
  1. 查看日志信息
logger.info("This is a info message")
logger.error("This is a error message")

通过以上设置,我们同样可以轻松实现屏蔽TensorFlow输出日志信息的效果。

示例一:

import logging
import tensorflow as tf

# 设置日志输出等级
logging.getLogger('tensorflow').setLevel(logging.ERROR)

# 输出日志
tf.logging.info("This is an info message")
tf.logging.debug("This is a debug message")
tf.logging.error("This is an error message")

以上代码将输出如下信息:

2019-04-08 11:02:37.375672: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-04-08 11:02:37.482534: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties: 
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.8685
pciBusID: 0000:08:00.0
totalMemory: 8.00GiB freeMemory: 6.60GiB
...
2019-04-08 11:02:40.122334: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x7fe372000750 executing computations on platform CUDA. Devices:
...
2019-04-08 11:02:40.126375: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): GeForce GTX 1080, Compute Capability 6.1
2019-04-08 11:02:40.127971: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Adding visible gpu devices: 0
2019-04-08 11:02:40.375124: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-04-08 11:02:40.375250: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988]      0 
2019-04-08 11:02:40.375296: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0:   N 
2019-04-08 11:02:40.375515: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x7fe372000750 executing computations on platform CUDA. Devices:
...
2019-04-08 11:02:40.385415: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): GeForce GTX 1080, Compute Capability 6.1
2019-04-08 11:02:41.196334: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Adding visible gpu devices: 0
...
2019-04-08 11:02:41.395196: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Adding visible gpu devices: 0
2019-04-08 11:02:41.395312: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-04-08 11:02:41.395356: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988]      0 
2019-04-08 11:02:41.395405: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0:   N 
2019-04-08 11:02:41.472729: W tensorflow/core/framework/op_kernel.cc:1279] OP_REQUIRES failed at transpose_op.cc:160 : Invalid argument: transpose expects a vector of size 4. But input(0) is a vector of size 8
2019-04-08 11:02:41.472802: W tensorflow/core/common_runtime/base_collective_executor.cc:217] BaseCollectiveExecutor::StartAbort Invalid argument: transpose expects a vector of size 4. But input(0) is a vector of size 8
     [[{{node tower_0_4/transpose_4}}]]
2019-04-08 11:02:41.536973: W tensorflow/core/framework/op_kernel.cc:1279] OP_REQUIRES failed at transpose_op.cc:160 : Invalid argument: transpose expects a vector of size 4. But input(0) is a vector of size 8
2019-04-08 11:02:41.537051: W tensorflow/core/common_runtime/base_collective_executor.cc:217] BaseCollectiveExecutor::StartAbort Invalid argument: transpose expects a vector of size 4. But input(0) is a vector of size 8
     [[{{node tower_0_4/transpose_4}}]]
2019-04-08 11:02:41.580132: W tensorflow/core/framework/op_kernel.cc:1279] OP_REQUIRES failed at transpose_op.cc:160 : Invalid argument: transpose expects a vector of size 4. But input(0) is a vector of size 8
2019-04-08 11:02:41.580203: W tensorflow/core/common_runtime/base_collective_executor.cc:217] BaseCollectiveExecutor::StartAbort Invalid argument: transpose expects a vector of size 4. But input(0) is a vector of size 8
     [[{{node tower_0_4/transpose_4}}]]

可以看到,由于TensorFlow默认输出等级为INFO,因此程序输出了大量的日志信息。但是由于我们设置了日志等级为ERROR,因此只有ERROR等级的日志信息被输出。这也就实现了屏蔽TensorFlow输出的log信息。

示例二:

import tensorflow as tf

# 创建自定义日志记录器
logger = tf.logging.Logger("my_logger")

# 设置日志等级
logger.setLevel(tf.logging.ERROR)

# 输出日志信息
logger.info("This is an info message")
logger.debug("This is a debug message")
logger.error("This is an error message")

以上代码的输出结果与示例一相同,因此这里不再重复给出。可以看到,使用TensorFlow提供的日志记录器同样可以实现屏蔽TensorFlow输出日志信息的效果。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:在tensorflow中实现屏蔽输出的log信息 - Python技术站

(0)
上一篇 2023年5月25日
下一篇 2023年5月25日

相关文章

  • 详细记一次Docker部署服务的爬坑历程

    详细记一次Docker部署服务的爬坑历程 概述 Docker是一种轻量级的虚拟化技术,可以将应用程序和其所需的依赖项打包到一个容器中,以便可以在任何地方运行。Docker部署服务比传统方式更加灵活和方便,但如果不注意一些要点就有可能遇到一些问题。在这篇文章中,我们将会分享如何在Docker中部署服务时的一些注意事项和一些可能会遇到的问题以及如何解决这些问题。…

    人工智能概览 2023年5月25日
    00
  • 详解微信小程序自定义组件的实现及数据交互

    下面我给出详解微信小程序自定义组件的实现及数据交互的完整攻略。内容分为以下几部分: 自定义组件的概念及基本用法 自定义组件的实现步骤 自定义组件与页面的数据交互 示例说明 1. 自定义组件的概念及基本用法 自定义组件是一种可以重复使用的自定义元素,由类似视图和逻辑的 WXML, WXSS 和 JS 结合而成。一般情况下,自定义组件的结构是由: wxml 文件…

    人工智能概论 2023年5月25日
    00
  • Freehand9中有什么新增功能 Freehand新增功能全程讲解

    Freehand9中有什么新增功能 Freehand9是一款功能强大的绘图工具,它可以帮助用户进行各种平面设计和UI设计。它在之前的版本上增加了一些新的功能,在这里我将介绍Freehand9中的新增功能。 ? 新增颜色板 Freehand9中增加了一个新的颜色板,它包含了更多的颜色和渐变,可以让用户更轻松地选择和编辑颜色。用户还可以自定义颜色,并将其保存到自…

    人工智能概览 2023年5月25日
    00
  • 使用Python自动化破解自定义字体混淆信息的方法实例

    使用Python自动化破解自定义字体混淆信息的方法需要以下步骤: 1. 获取字体文件 首先,需要获取目标网站使用的自定义字体文件。可以通过审查元素查看CSS样式表中对字体文件的引用,或是通过抓包工具获取字体文件的URL地址。 2. 解析字体文件 使用 FontTools 库解析字体文件,利用其中的 ttLib.TTFont 类读取字体文件,提取字体映射(Gl…

    人工智能概论 2023年5月25日
    00
  • 简单不求人 轻松让你击破ATA硬盘密码

    简单不求人 轻松让你击破ATA硬盘密码 什么是ATA硬盘密码 ATA(Advanced Technology Attachment)硬盘密码是一种硬件层面的安全措施,能够加密并保护硬盘中的数据。只有在输入正确密码之后,才能使用这个硬盘。 准备工作 为了攻破ATA硬盘密码,你需要准备以下工具: 一个 ATA-to-USB转换器,或者一个已经安装好ATA接口的计…

    人工智能概览 2023年5月25日
    00
  • 如何将tensorflow训练好的模型移植到Android (MNIST手写数字识别)

    关于如何将 TensorFlow 训练好的模型移植到 Android 上,我将分以下几个步骤进行介绍: 导出模型 在使用 TensorFlow 进行模型训练并完成后,需要将模型导出,以便在 Android 上进行使用。导出模型时,需要定义保存路径和需要导出的节点信息,示例代码如下: from tensorflow.python.framework impor…

    人工智能概论 2023年5月24日
    00
  • Balabolka怎么用?Balabolka使用教程以及常见问题解决教程

    Balabolka使用教程 Balabolka是一款功能强大的朗读软件,它可以将文字材料转化为语音朗读,方便用户在不依赖视觉的情况下获取信息。它支持多种文本格式,并支持多种语音引擎,读音清晰自然,使用简单方便。接下来我们来看看如何使用Balabolka。 下载和安装Balabolka 首先,我们需要下载和安装Balabolka软件。您可以在官方网站 http…

    人工智能概览 2023年5月25日
    00
  • C语言 fseek(f,0,SEEK_SET)函数案例详解

    C语言 fseek(f,0,SEEK_SET)函数案例详解 简介 在C语言中,fseek()函数用于移动指定文件流的文件指针。其中,文件指针是指向文件中特定位置的指针,以便读取或写入某个特定位置的数据。fseek()函数的原型如下: int fseek(FILE *stream, long int offset, int whence); 其中,stream…

    人工智能概览 2023年5月25日
    00
合作推广
合作推广
分享本页
返回顶部