Python sides effects such as printing, mutating a list or a global variable, inside of a tensorflow.function
may not behave as
expected. Because of the Rules of tracing, the execution of side effects will
depend on the input values of the function and will execute only once per tracing.
import tensorflow as tf
@tf.function
def f(x):
print("A side effect", x)
f(1) # prints "A side effect 1"
f(1) # does not print anything
f(2) # prints "A side effect 2"
The example above depicts the issue encountered when using Python side effects in a tensorflow.function
. As a single trace is created
per input values, the second call to f(1)
does not output anything to the console.
The best practice would be to avoid using Python side effects and prefer the usage of the TensorFlow API with functions such as
tf.print
or tf.TensorArray`.