Suppression of deprecation warnings in Tensorflow Python

Hey guys, this article will be focussing on the deprecation warnings in TensorFlow as well as the logging information.

As with the new update of TensorFlow, from 1.x to 2.x, the deprecation warnings have been removed by default but still, some logging information is provided. As fas far, TF 1.x is concerned, we still need to work upon the deprecation warnings and informational loggings.

These warnings can occur while you are working with Tensorflow or training or testing models using Tensorflow. You can also refer to the basics of TensorFlow.

Implementation for suppression of deprecation in Tensorflow:-

For TF 1.x, the following code can be used to remove logging information:-

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

For TF 2.x:-

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

Or we can change the contrib warnings to none to stop prevent warnings to get printed:-

import tensorflow as tf
if type(tf.contrib) != type(tf): tf.contrib._warning = None

Else we can add flexibility to our work by using the log_level codes to choose what to show and what not to:-

import os
import tensorflow as tf
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
where:- 0 = all messages are logged.
     1= INFO logs are removed.
              2 = INFO with WARNINGS is removed.
        3= ALL messages are removed.

Now its time to remove the deprecated warnings for TF 1.x as 2.x already handles it without displaying. So this code remove all the deprecated warnings:-

from tensorflow.python.util import deprecation
deprecation._PRINT_DEPRECATION_WARNINGS = False

Or if we want future warnings to be suppressed too with the current deprecated warnings, the following can be used:-

import warnings
warnings.filterwarnings('ignore', category=DeprecationWarning)
warnings.filterwarnings('ignore', category=FutureWarning)

I hope you liked this article, and do check our other posts.

Thank you

Leave a Reply

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