Press "Enter" to skip to content

Turning off the annoying stdout messages from TensorFlow 2.0rc

Disclaimer: I only use the GPU version of TensorFlow v2.0.0-rc1, so I don’t know if this works with other versions.

When writing the TensorFlow code in Python scripts and running the scripts in a terminal, we usually get a bunch of messages in stdout. The messages log the information of the initialization stage of TensorFlow. These messages are annoying to many people. The messages come from both the underlying C++ code and the Python code of TensorFlow. So we need to disable the two sources of the messages in different ways.

If we install TensorFlow through conda or pip, then the C++ code is already compiled, and we don’t have any way to modify the C++ source code. Fortunately, the messages coming from this source can be controlled by an environment variable, TF_CPP_MIN_LOG_LEVEL.  In Linux, before running Python scripts, we can simply do something like $ export TF_CPP_MIN_LOG_LEVEL=2. But I prefer to hard code this environment variable in my Python scripts because I’m lazy. I add the following code in my Python scripts before the code uses any TensorFlow object or function:


import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"

This code does not necessarily need to be placed before import tensorflow. But it must be placed before the code encounters any object or function coming from TensorFlow. I think the initialization of the TensorFlow package does not happen before this point.

The second source of the messages can be turned off by the standard Python logging system:


import logging
logging.getLogger("tensorflow").setLevel(logging.ERROR)
logging.getLogger("tensorflow").addHandler(logging.NullHandler(logging.ERROR))

7 Comments

  1. Albert Albert

    os.environ[“TF_CPP_MIN_LOG_LEVEL”] = 2 produces an error: “expecting str, not int”. So the way it works for me is: os.environ[“TF_CPP_MIN_LOG_LEVEL”] = “2”

    • Yes. You’re right. It has to be a string. I fixed it in this post. Thank you!

  2. IlkayW IlkayW

    I tried so many things including your advice … but still it’s not suppressed.
    I don’t know what to do… it’s really annoying and quite tilting.

    My setting
    python 3.7
    tensorflow version: 2.1.0
    Spyder 4.1.4

    Do you know what I could do?

    Greetings,
    Ilkay

    • Hi Ilkay, what kind of warning messages were you getting from tensorflow?

      • IlkayW IlkayW

        2020-09-02 09:21:41.007603: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
        2020-09-02 09:21:42.774448: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
        2020-09-02 09:21:42.795887: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties:
        pciBusID: 0000:01:00.0 name: GeForce RTX 2080 computeCapability: 7.5
        coreClock: 1.71GHz coreCount: 46 deviceMemorySize: 8.00GiB deviceMemoryBandwidth: 417.23GiB/s
        2020-09-02 09:21:42.797785: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
        2020-09-02 09:21:42.803610: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
        2020-09-02 09:21:42.807019: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
        2020-09-02 09:21:42.808226: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
        2020-09-02 09:21:42.812423: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
        2020-09-02 09:21:42.814652: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
        2020-09-02 09:21:42.822271: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
        2020-09-02 09:21:42.822677: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
        2020-09-02 09:21:42.845317: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
        2020-09-02 09:21:42.846307: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties:
        pciBusID: 0000:01:00.0 name: GeForce RTX 2080 computeCapability: 7.5
        coreClock: 1.71GHz coreCount: 46 deviceMemorySize: 8.00GiB deviceMemoryBandwidth: 417.23GiB/s
        2020-09-02 09:21:42.846995: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
        2020-09-02 09:21:42.847343: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
        2020-09-02 09:21:42.847687: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
        2020-09-02 09:21:42.848035: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
        2020-09-02 09:21:42.848380: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
        2020-09-02 09:21:42.848729: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
        2020-09-02 09:21:42.849085: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
        2020-09-02 09:21:42.849455: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
        2020-09-02 09:21:43.362098: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1096] Device interconnect StreamExecutor with strength 1 edge matrix:
        2020-09-02 09:21:43.362477: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] 0
        2020-09-02 09:21:43.362702: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] 0: N
        2020-09-02 09:21:43.363078: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1241] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6269 MB memory) -> physical GPU (device: 0, name: GeForce RTX 2080, pci bus id: 0000:01:00.0, compute capability: 7.5)

        • I upgraded my TF to version 2.3 and found that some behaviors have been changed. Now I have to put these lines of code completely before import tensorflow (or any lines importing modules from tensorflow).

          Have you tried to put these lines at the very beginning of your source files? At least it worked for me on Linux.

  3. Your suggestion of suppressing the C++ code’s output by using the `TF_CPP_MIN_LOG_LEVEL` variable is very helpful. But I’m really curious: does setting this variable to `”2″` universally suppress all unnecessary initialization messages, or are there certain messages that might still slip through?

    I, too, often find these initialization messages a little overwhelming. It’s so fascinating to understand that these messages originate from both the C++ and Python components of TensorFlow. I previously thought the output merely mirrored Python’s behavior.

Leave a Reply to PY Chuang Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.