Python 有没有办法抑制 TensorFlow 打印的消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40426502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:30:57  来源:igfitidea点击:

Is there a way to suppress the messages TensorFlow prints?

pythontensorflow

提问by

I think that those messages are really important for the first few times but then it is just useless. It is actually making things worse to read and debug.

我认为这些信息在前几次确实很重要,但后来就毫无用处了。它实际上使阅读和调试变得更糟。

I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcublas.so.8.0 locally I tensorflow/stream_executor/dso_loader.cc:119] Couldn't open CUDA library libcudnn.so. LD_LIBRARY_PATH: I tensorflow/stream_executor/cuda/cuda_dnn.cc:3459] Unable to load cuDNN DSO I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcufft.so.8.0 locally I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcuda.so.1 locally I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcurand.so.8.0 locally

I tensorflow/stream_executor/dso_loader.cc:128] 在本地成功打开了 CUDA 库 libcublas.so.8.0 I tensorflow/stream_executor/dso_loader.cc:119] 无法打开 CUDA 库 libcudnn.so。LD_LIBRARY_PATH: I tensorflow/stream_executor/cuda/cuda_dnn.cc:3459] 无法加载 cuDNN DSO I tensorflow/stream_executor/dso_loader.cc:128] 在本地成功打开 CUDA 库 libcufft.so.8.0 我 tensorflow/stream_loader.cc 128] 在本地成功打开 CUDA 库 libcuda.so.1 I tensorflow/stream_executor/dso_loader.cc:128] 在本地成功打开 CUDA 库 libcurand.so.8.0

Is there a way to suppress the ones that just say it was successful?

有没有办法压制那些只说成功的人?

回答by craymichael

UPDATE(beyond 1.14): see my more thorough answer here (this is a dupe question anyway): https://stackoverflow.com/a/38645250/6557588

更新(超过 1.14):在这里看到我更彻底的答案(无论如何这是一个欺骗性的问题):https: //stackoverflow.com/a/38645250/6557588

In addition to Wintro's answer, you can also disable/suppress TensorFlow logs from the C side (i.e. the uglier ones starting with single characters: I, E, etc.); the issueopen regarding logging has been updated to state that you can now control logging via an environmental variable. You can now change the level by setting the environmental variable called TF_CPP_MIN_LOG_LEVEL; it defaults to 0 (all logs shown), but can be set to 1 to filter out INFOlogs, 2 to additionally filter out WARNINGlogs, and 3 to additionally filter out ERRORlogs. It appears to be in master now, and will likely be a part of future version (i.e. versions after r0.11). See this pagefor more information. Here is an example of changing the verbosity using Python:

除了 Wintro 的回答,您还可以从 C 端禁用/抑制 TensorFlow 日志(即以单个字符开头的丑陋日志:I、E 等);关于日志记录的未解决问题已更新为说明您现在可以通过环境变量控制日志记录。您现在可以通过设置名为TF_CPP_MIN_LOG_LEVEL;的环境变量来更改级别。默认为 0(显示所有日志),但可以设置为 1 以过滤掉INFO日志,设置为2 以额外过滤掉WARNING日志,而设置为 3 以额外过滤掉ERROR日志。它现在似乎在 master 中,并且可能会成为未来版本的一部分(即 r0.11 之后的版本)。有关更多信息,请参阅此页面。以下是使用 Python 更改详细程度的示例:

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

回答by Ziyad Edher

You can set the verbosity levels of TensorFlow's logging using

您可以使用以下命令设置 TensorFlow 日志记录的详细级别

tf.logging.set_verbosity(tf.logging.ERROR)

where ERRORcan be any of DEBUG, INFO, WARN, ERROR, or FATAL. See the logging module.

whereERROR可以是DEBUG, INFO, WARN, ERROR, 或 中的任何一个FATAL。请参阅日志记录模块

However, setting this to ERRORdoes not always completely block all INFOlogs, to completely block them you have two main choices in my opinion.

但是,将其设置为ERROR并不总是完全阻止所有INFO日志,要完全阻止它们,我认为您有两个主要选择。

  • If you are using Linux, you can just grepout all output strings beginning with I tensorflow/.
  • Otherwise, you can completely rebuild TensorFlow with some modified files. See this answer.
  • 如果您使用的是 Linux,则可以仅grep输出所有以I tensorflow/.
  • 否则,您可以使用一些修改过的文件完全重建 TensorFlow。看到这个答案

If you're using TensorFlow version 1 (1.X), you can use

如果您使用的是 TensorFlow 版本 1 (1.X),则可以使用

tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

回答by Kris

As of Tensorflow v1.14(yes, including version 2.x) you can use the native logging module to silence Tensorflow:

Tensorflow v1.14(是的,包括2.x版)开始,您可以使用本机日志记录模块来使 Tensorflow 静音:

import logging
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'  # FATAL
logging.getLogger('tensorflow').setLevel(logging.FATAL)


I personally use this in my projects:

我个人在我的项目中使用它:

def set_tf_loglevel(level):
    if level >= logging.FATAL:
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    if level >= logging.ERROR:
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    if level >= logging.WARNING:
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
    else:
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '0'
    logging.getLogger('tensorflow').setLevel(level)

so that I can disable tf logging by running:

这样我就可以通过运行来禁用 tf 日志记录:

set_tf_loglevel(logging.FATAL)

and I can re-enable with

我可以重新启用

set_tf_loglevel(logging.INFO)

回答by Adam Wallner

I created a function which shuts TF up. I call it on start of my programs. Some messages are very annoying and I cannot do anything about them...

我创建了一个关闭 TF 的函数。我在我的程序开始时调用它。有些消息非常烦人,我对此无能为力...

def tensorflow_shutup():
    """
    Make Tensorflow less verbose
    """
    try:
        # noinspection PyPackageRequirements
        import os
        from tensorflow import logging
        logging.set_verbosity(logging.ERROR)
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

        # Monkey patching deprecation utils to shut it up! Maybe good idea to disable this once after upgrade
        # noinspection PyUnusedLocal
        def deprecated(date, instructions, warn_once=True):
            def deprecated_wrapper(func):
                return func
            return deprecated_wrapper

        from tensorflow.python.util import deprecation
        deprecation.deprecated = deprecated

    except ImportError:
        pass

回答by Danibix

Considering previous answers, in Tensorflow 1.14 is actually possible to eliminate all the message generated by the library by including in the code the following two lines:

考虑到之前的答案,在 Tensorflow 1.14 中实际上可以通过在代码中包含以下两行来消除库生成的所有消息:

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

This method exploits both the environment variable approach and Tensorflow logging module.

该方法同时利用了环境变量方法和 Tensorflow 日志记录模块。

Note:the compatibility version is necessary to avoids further warnings given by the library, since the standard one is now deprecated.

注意:兼容性版本是必要的,以避免库给出进一步的警告,因为标准版本现已弃用。