TensorFlow
warnings
Python
error-handling
machine-learning

How to Suppress Tensorflow warning displayed in result

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

TensorFlow warnings come from more than one place, so there is no single switch that hides all of them. Some messages come from TensorFlow's C++ runtime, some are normal Python warnings, and others are emitted through Python logging. The right fix depends on which layer is printing the message.

Suppress C++ TensorFlow Logs with TF_CPP_MIN_LOG_LEVEL

The most common way to reduce noisy TensorFlow startup messages is setting TF_CPP_MIN_LOG_LEVEL before importing TensorFlow.

python
1import os
2
3os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
4
5import tensorflow as tf
6
7print(tf.__version__)

Typical values are:

  • '0 shows everything'
  • '1 hides INFO messages'
  • '2 hides INFO and WARNING messages'
  • '3 hides INFO, WARNING, and ERROR messages'

In practice, 2 is the common setting when you want a quieter console without hiding serious failures.

The order matters. If you set the environment variable after importing TensorFlow, many messages have already been emitted.

Control Python-Side Logging

TensorFlow also uses Python logging for some messages. You can lower that verbosity through tf.get_logger().

python
1import os
2os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
3
4import logging
5import tensorflow as tf
6
7tf.get_logger().setLevel(logging.ERROR)
8
9print("logger level updated")

This is useful when the message is coming from TensorFlow's Python layer rather than the lower-level runtime.

If you only want to suppress warning-level logs but still keep errors visible, use logging.ERROR as shown above.

Filter Standard Python Warnings Separately

Some warnings are ordinary Python warnings, such as DeprecationWarning or FutureWarning. Those are handled through the warnings module, not TensorFlow logging settings.

python
1import warnings
2
3warnings.filterwarnings("ignore", category=FutureWarning)
4
5import tensorflow as tf

You can also target messages more narrowly:

python
1import warnings
2
3warnings.filterwarnings(
4    "ignore",
5    message=".*deprecated.*",
6    category=UserWarning,
7)

This is safer than suppressing all warnings globally because it keeps unrelated warnings visible.

Use Environment Variables from the Shell When Needed

If you do not control the Python source directly, set the log level in the shell before launching the program.

bash
export TF_CPP_MIN_LOG_LEVEL=2
python train.py

On Windows Command Prompt:

bat
set TF_CPP_MIN_LOG_LEVEL=2
python train.py

This approach is useful for notebooks launched by scripts, CI jobs, or wrappers where the import order is not easy to change.

Suppress Carefully in Notebooks and Production

It is reasonable to reduce noise in demos, notebooks, and production logs. It is much less reasonable to silence everything during debugging. A warning about deprecated APIs, missing GPU libraries, or graph fallback behavior may be exactly what tells you why training is slow or unstable.

A balanced setup often looks like this:

python
1import os
2import logging
3import warnings
4
5os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
6warnings.filterwarnings("ignore", category=FutureWarning)
7
8import tensorflow as tf
9
10tf.get_logger().setLevel(logging.ERROR)

That hides routine clutter while still leaving real exceptions intact.

Know the Difference Between Warning and Error

Suppressing warnings does not fix the underlying issue. For example, if TensorFlow warns about a missing CUDA library, hiding the message will not make GPU acceleration start working. If it warns that an API is deprecated, the code still needs to be updated eventually.

Treat suppression as an output-control tool, not as a substitute for diagnosis.

Common Pitfalls

The most common mistake is setting TF_CPP_MIN_LOG_LEVEL after importing TensorFlow, which is too late for many startup messages. Another frequent issue is using the warnings module to suppress messages that are actually coming from TensorFlow logging, or vice versa. Developers also overuse TF_CPP_MIN_LOG_LEVEL=3, which can hide messages they later wish they had kept. Finally, silencing warnings in a notebook may make the output cleaner, but it can also hide deprecation and environment clues that matter during upgrades.

Summary

  • Use TF_CPP_MIN_LOG_LEVEL before importing TensorFlow to reduce C++ runtime log noise.
  • Use tf.get_logger().setLevel(...) for TensorFlow's Python-side logger.
  • Use the warnings module for standard Python warnings such as FutureWarning.
  • Shell environment variables are useful when you cannot edit the import order in code.
  • Suppress warnings selectively so you do not hide messages that point to real problems.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.