【问题标题】:Is there a way to suppress the messages TensorFlow prints?有没有办法抑制 TensorFlow 打印的消息?
【发布时间】:2017-03-18 12:19:28
【问题描述】:

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

我的tensorflow/stream_executor/dso_loader.cc:128]成功打开 CUDA 库 libcublas.so.8.0 本地我 tensorflow/stream_executor/dso_loader.cc:119] 无法打开 CUDA 库 libcudnn.so。 LD_LIBRARY_PATH:我 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_executor/dso_loader.cc:128]成功打开CUDA 本地库 libcuda.so.1 我 tensorflow/stream_executor/dso_loader.cc:128]成功打开CUDA 本地库 libcurand.so.8.0

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

【问题讨论】:

标签: python tensorflow


【解决方案1】:

您可以使用

设置 TensorFlow 日志记录的详细程度
tf.logging.set_verbosity(tf.logging.ERROR)

其中ERROR 可以是DEBUG、INFO、WARN、ERROR 或FATAL 中的任何一个。见the logging module。

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

  • 如果您使用的是 Linux,您只需 grep 输出所有以 I tensorflow/ 开头的输出字符串。
  • 否则,您可以使用一些修改过的文件完全重建 TensorFlow。见this answer。

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

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

【讨论】:

  • 在 tensorflow 2.0 中,我收到了 module 'tensorflow' has no attribute 'logging'
  • @ProQ 查看我的答案here - 简而言之,改用tf.get_logger(),或tf.compat.v1.logging(不推荐),以及TF_CPP_MIN_LOG_LEVEL 变量
【解决方案2】:

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

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

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

您可以在运行脚本的环境中设置此环境变量。例如,对于 bash,这可以在文件 ~/.bashrc、/etc/environment、/etc/profile 中,或者在实际的 shell 中为:

TF_CPP_MIN_LOG_LEVEL=2 python my_tf_script.py

【讨论】:

    【解决方案3】:

    我创建了一个关闭 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
    

    编辑:这适用于 TF 2.0 及更高版本:

    def tensorflow_shutup():
        """
        Make Tensorflow less verbose
        """
        try:
            os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    
            # noinspection PyPackageRequirements
            import tensorflow as tf
            from tensorflow.python.util import deprecation
    
            tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
    
            # 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):  # pylint: disable=unused-argument
                def deprecated_wrapper(func):
                    return func
                return deprecated_wrapper
    
            deprecation.deprecated = deprecated
    
        except ImportError:
            pass
    

    【讨论】:

    • 强大的决心
    • 酷但不起作用..仍然相同的消息:2021-04-29 21:54:07.369239: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] 成功打开动态库 cudart64_110 .dll
    • @JiriOtoupalイりオトウパー 它对我有用。我已经为 TF 2.x 更新了它。只需要在导入tensorflow之前调用这个函数!
    【解决方案4】:

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

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

    此方法利用环境变量方法和 Tensorflow 日志记录模块。

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

    【讨论】:

      【解决方案5】:

      从 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)
      

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

      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)
      
      

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

      set_tf_loglevel(logging.FATAL)
      

      我可以重新启用

      set_tf_loglevel(logging.INFO)
      

      【讨论】:

        【解决方案6】:

        对于 tensorflow 2.2,您可以使用以下行禁用日志记录:

        import tensorflow as tf   
        tf.get_logger().setLevel('ERROR')
        

        【讨论】:

          【解决方案7】:

          对于仍在努力让 os.environ 解决方案像我一样工作的任何人,请检查它是否放置在 您在脚本中导入 tensorflow 之前,就像 craymichael 的回答一样:

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

          【讨论】:

            【解决方案8】:

            这对我有用,无需引入 TensorFlow 本身以外的任何包:

            import tensorflow as tf
            
            tf.autograph.set_verbosity(0) # "0" means no logging.
            

            更多详情,请查看此 TensorFlow API documentation。

            【讨论】:

            • 谢谢,我在 W10 上收到了关于无法调用 ptxas 的 2.2.0 的不重要的 XLA 警告,这是必要的解决方案。
            【解决方案9】:

            This 对我有用:

            import logging
            logging.getLogger('tensorflow').setLevel(logging.ERROR)
            os.environ["KMP_AFFINITY"] = "noverbose"
            os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
            import tensorflow as tf
            tf.autograph.set_verbosity(3)
            

            【讨论】:

              猜你喜欢
              • 2019-01-16
              • 2016-11-03
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-11-18
              相关资源
              最近更新 更多