org.sonar.l10n.py.rules.python.S6928.html Maven / Gradle / Ivy
This rule raises an issue when a Python side effect happens inside a tensorflow.function
.
Why is this an issue?
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`.
How to fix it
To fix this issue either remove the side effect or use the corresponding TensorFlow function.
Code examples
Noncompliant code example
import tensorflow as tf
@tf.function
def f(x):
print("Printing", x) # Noncompliant print is a side effect
Compliant solution
import tensorflow as tf
@tf.function
def f(x):
tf.print("Printing", x) # Compliant
Resources
Documentation
- TensorFlow Documentation - Executing Python side effects
- TensorFlow Documentation - tf.print reference
- TensorFlow Documentation - tf.summary reference
- TensorFlow Documentation - tf.Variable methods reference
- TensorFlow Documentation - tf.TensorArray reference
- TensorFlow Documentation - tf.data reference