All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.py.rules.python.S6928.html Maven / Gradle / Ivy

There is a newer version: 4.23.0.17664
Show newest version

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





© 2015 - 2024 Weber Informatics LLC | Privacy Policy