Last updated: Apr 10, 2024
Reading time·5 min
Use the os.environ
module to set the TF_CPP_MIN_LOG_LEVEL
environment
variable to 3
to disable/suppress all tensorflow
warnings.
When the environment variable is set to 3
, info, warning and error messages
are not logged.
# 👇️ Disable `tensorflow` warnings import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tensorflow as tf print("TensorFlow version:", tf.__version__)
Make sure to add the import statement of the
os module and the line that
sets the TF_CPP_MIN_LOG_LEVEL
environment variable above your tensorflow
import in the file.
Running the code sample above produces the following output.
If I remove the import of the os
module and the line that sets the environment
variable, the following output is produced.
Note that the import statement of the os
module and the line that sets the
environment variable has to be placed above the line that imports
tensorflow.
The TF_CPP_MIN_LOG_LEVEL
environment variable is used to control TensorFlow's
log level.
TF_CPP_MIN_LOG_LEVEL value | Description |
---|---|
"0" | All messages are logged. |
"1" | Logs all messages except INFO . |
"2" | Logs all messages except INFO and WARNING . (default) |
"3" | Logs all messages except INFO , WARNING and ERROR . |
By default the log level is set to "2"
.
TF_CPP_MIN_LOG_LEVEL
environment variable via the command lineAn alternative to setting the TF_CPP_MIN_LOG_LEVEL
environment variable in
your code is to set it on your machine.
For example, you could use the command line to set the environment variable from your terminal before starting your Python application.
On macOS and Linux, run the following command in bash
or zsh
.
# On macOS and Linux export TF_CPP_MIN_LOG_LEVEL="3"
On Windows, you could run the following command in CMD.
set TF_CPP_MIN_LOG_LEVEL="3"
If you use PowerShell on Windows, run the following command instead.
$env:TF_CPP_MIN_LOG_LEVEL="3"
Now you can remove the import of the os
module and the code that sets the
environment variable.
import tensorflow as tf print("TensorFlow version:", tf.__version__)
However, note that when using this approach, the environment variable is only set for the current shell session.
You could also set the environment variable permanently.
TF_CPP_MIN_LOG_LEVEL
environment variableIf you are on macOS or Linux, you can set the TF_CPP_MIN_LOG_LEVEL
environment
variable in your profile file.
Every time you start your computer, your profile file (e.g. ~/.bashrc
or
~/.zshrc
) will run and the environment variable will get set.
For example, if you use bash
, you can add the variables to ~/.bashrc
or
~/.bash_profile
.
# With Gedit sudo gedit ~/.bashrc # Or with Nano sudo nano ~/.bashrc
Add the following line at the bottom of your ~/.bashrc
file.
export TF_CPP_MIN_LOG_LEVEL="3"
Then, source the updated ~/.bashrc
file and restart your server.
# BASH source ~/.bashrc source ~/.bash_profile
If you use zsh
, you have to add the environment variable to your ~/.zshrc
file instead.
sudo gedit ~/.zshrc sudo nano ~/.zshrc
Paste the following line at the bottom of your ~/.zshrc
file.
export TF_CPP_MIN_LOG_LEVEL="3"
Source your ~/.zshrc
file and restart your server.
source ~/.zshrc
Now you won't see any warning messages when running your TensorFlow code.
TF_CPP_MIN_LOG_LEVEL
environment variableIf you want to set the TF_CPP_MIN_LOG_LEVEL
environment variable or other
environment variables permanently on Windows:
Click on the "Environment Variables" button.
In the "System variables" section, click on the New... button.
Set TF_CPP_MIN_LOG_LEVEL
as the name of the variable and set its value to
3
.
Click on OK to apply the changes and restart your terminal and development server.
get_logger()
methodYou can also use the get_logger()
method from the tensorflow
module to
disable the TensorFlow warnings.
import tensorflow as tf tf.get_logger().setLevel('ERROR') print("TensorFlow version:", tf.__version__)
The tf.get_logger method has 5 levels of logging from the most serious to the least:
When using this approach you might still see some info messages being printed.
You could also set the TF_CPP_MIN_LOG_LEVEL
environment variable to suppress
all warnings.
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tensorflow as tf tf.get_logger().setLevel('ERROR') print("TensorFlow version:", tf.__version__)
Another thing you can try is to directly use the logging
module to suppress
warning messages in TensorFlow.
import logging logging.getLogger("tensorflow").setLevel(logging.WARNING) import tensorflow as tf print("TensorFlow version:", tf.__version__)
Note that the line that calls the getLogger()
method has to be placed above
your tensorflow
import statement.
You can also try to use the set_verbosity() method in TensorFlow version 2.
import tensorflow as tf tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR) print("TensorFlow version:", tf.__version__)
You can also use the AUTOGRAPH_VERBOSITY environment variable to control the log level of autograph log messages.
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tensorflow as tf os.environ['AUTOGRAPH_VERBOSITY'] = '0' # Autograph verbosity is now 0 os.environ['AUTOGRAPH_VERBOSITY'] = '3' # Autograph verbosity is now 3 tf.get_logger().setLevel('ERROR') print("TensorFlow version:", tf.__version__)
The AUTOGRAPH_VERBOSITY
environment variable can be set to an integer. Larger
values mean increased verbosity (e.g. 6-10), whereas 0
means no logging.
When debugging your application, you could try setting the environment variable to 8-10 to increase the verbosity.
silence_tensorflow
module to disable TensorFlow's warningsIf none of the suggestions helped, you can always use the silence_tensorflow module to suppress TensorFlow's warnings.
First, install the module by running the following command from your terminal.
pip install silence_tensorflow # Or with pip3 pip3 install silence_tensorflow # Or with python -m python -m pip install silence_tensorflow python3 -m pip install silence_tensorflow
Then import the silence_tensorflow
function from the module and call it above
your tensorflow
import.
from silence_tensorflow import silence_tensorflow silence_tensorflow() import tensorflow as tf print("TensorFlow version:", tf.__version__)
Note that the silence_tensorflow()
function has to be called before you import
tensorflow
.
You can also import and initialize the module in a single line.
import silence_tensorflow.auto import tensorflow as tf print("TensorFlow version:", tf.__version__)
If you use Pylint, you might have to suppress the warning for the unused import statement.
import silence_tensorflow.auto # pylint: disable=unused-import import tensorflow as tf print("TensorFlow version:", tf.__version__)
As long as the import statement is placed above the line that imports
tensorflow
, you won't get any warnings messages.
You can learn more about the related topics by checking out the following tutorials: