5

I have a for loop with several different deep learning models in it that generates this warning:

WARNING:tensorflow:5 out of the last 5 calls to <function Model.make_predict_function.<locals>.predict_function at 0x000001B0A8CC90D0> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for  more details.
WARNING:tensorflow:6 out of the last 6 calls to <function Model.make_predict_function.<locals>.predict_function at 0x000001B0A6C01940> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for  more details.

I have tried many different things in the for loop to stop it from popping up with no success. Is there simply a way to disable all warnings?

Cole
  • 319
  • 2
  • 5
  • 10
  • 2
    This link should help with your question. https://stackoverflow.com/questions/35911252/disable-tensorflow-debugging-information#:~:text=So%20to%20knock%20out%20these,resetwarnings()%20. – Justhackingx Dec 21 '21 at 02:23

3 Answers3

8

You can use this to avoid warnings:

import os
import tensorflow as tf

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
MSH
  • 1,743
  • 2
  • 14
  • 22
Kapil Musale
  • 223
  • 1
  • 4
  • 1
    In detail:- 0 = all messages are logged (default behavior) , 1 = INFO messages are not printed , 2 = INFO and WARNING messages are not printed , 3 = INFO, WARNING, and ERROR messages are not printed – Kapil Musale Dec 21 '21 at 07:53
  • This one doesn't work with "Unable to restore custom metric" warning whereas s510's solution does. – Krzysztof Maliszewski Jun 20 '23 at 09:31
6

Use this:

tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
Sibtain Reza
  • 513
  • 2
  • 14
4

Just set tensorflow logger to ERROR:

import tensorflow as tf
tf.get_logger().setLevel('ERROR')
s510
  • 2,271
  • 11
  • 18