ChatGPT解决这个技术问题 Extra ChatGPT

Is there a way to suppress the messages TensorFlow prints?

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

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


c
craymichael

UPDATE (beyond 1.14): see my more thorough answer here (this is a dupe question anyway): 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 issue open 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 INFO logs, 2 to additionally filter out WARNING logs, and 3 to additionally filter out ERROR logs. It appears to be in master now, and will likely be a part of future version (i.e. versions after r0.11). See this page for more information. Here is an example of changing the verbosity using Python:

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

You can set this environmental variable in the environment that you run your script in. For example, with bash this can be in the file ~/.bashrc, /etc/environment, /etc/profile, or in the actual shell as:

TF_CPP_MIN_LOG_LEVEL=2 python my_tf_script.py

m
mikey

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

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

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

However, setting this to ERROR does not always completely block all INFO logs, to completely block them you have two main choices in my opinion.

If you are using Linux, you can just grep out all output strings beginning with I tensorflow/.

Otherwise, you can completely rebuild TensorFlow with some modified files. See this answer.

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

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

In tensorflow 2.0 I'm getting module 'tensorflow' has no attribute 'logging'
@ProQ Check out my answer here - in short use tf.get_logger() instead, or tf.compat.v1.logging (not recommended), as well as the TF_CPP_MIN_LOG_LEVEL variable
N
Nicolas Gervais

As of Tensorflow v1.14 (yes, including version 2.x) you can use the native logging module to silence 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:

set_tf_loglevel(logging.FATAL)

and I can re-enable with

set_tf_loglevel(logging.INFO)

A
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...

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

Edit: This is for TF 2.0 and up:

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

formidable determination
Cool but does not work.. still same message: 2021-04-29 21:54:07.369239: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
@JiriOtoupalイりオトウパー It works for me. I've updated it for TF 2.x. You just need to call this function before importing tensorflow!
M
Matt_Haythornthwaite

To anyone still struggling to get the os.environ solution to work as I was, check that this is placed before you import tensorflow in your script, just like craymichael's answer:

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

a
alex

This is what worked for me:

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)

D
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:

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.

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


W
Wilmar van Ommeren

For tensorflow 2.2 you can disable the logging with the following lines:

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

L
Li-Pin Juan

This works for me without introducing any packages other than TensorFlow itself:

import tensorflow as tf

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

For more details, check this TensorFlow API documentation.


Thanks, I was getting unimportant XLA warnings with 2.2.0 on W10 about being unable to invoke ptxas and this was the necessary solution.

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now