Is there a way to suppress the messages TensorFlow prints?

PythonTensorflow

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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)

Solution 3 - Python

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)

Solution 4 - Python

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

Solution 5 - Python

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

Solution 6 - Python

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)

Solution 7 - Python

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.

Solution 8 - Python

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

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

Solution 9 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser7115784View Question on Stackoverflow
Solution 1 - PythoncraymichaelView Answer on Stackoverflow
Solution 2 - PythonZiyad EdherView Answer on Stackoverflow
Solution 3 - PythonKrisView Answer on Stackoverflow
Solution 4 - PythonAdam WallnerView Answer on Stackoverflow
Solution 5 - PythonMatt_HaythornthwaiteView Answer on Stackoverflow
Solution 6 - PythonalexView Answer on Stackoverflow
Solution 7 - PythonDanibixView Answer on Stackoverflow
Solution 8 - PythonWilmar van OmmerenView Answer on Stackoverflow
Solution 9 - PythonLi-Pin JuanView Answer on Stackoverflow