0%

Suppress TensorFlow Warnings

To suppress TensorFlow warnings (in a single python file, or in the __init__.py of a package that depends on tensorflow):
[credit]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# replace `import tensorflow as tf` with this line
# or insert this line at the beginning of the `__init__.py` of a package that depends on tensorflow
tf = import_tensorflow()

# may put this function in another utility file
def import_tensorflow():
# Filter tensorflow version warnings
import os
# https://stackoverflow.com/questions/40426502/is-there-a-way-to-suppress-the-messages-tensorflow-prints/40426709
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # or any {'0', '1', '2'}
import warnings
# https://stackoverflow.com/questions/15777951/how-to-suppress-pandas-future-warning
warnings.simplefilter(action='ignore', category=FutureWarning)
warnings.simplefilter(action='ignore', category=Warning)
import tensorflow as tf
tf.get_logger().setLevel('INFO')
tf.autograph.set_verbosity(0)
import logging
tf.get_logger().setLevel(logging.ERROR)
return tf