Tensorflow control sources of randomness Medium

Detects if a random seed is set before random number generation. Setting a seed is important for improving reproducibility and avoiding non-determinism.

Detector ID
python/tensorflow-control-sources-of-randomness@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-

Noncompliant example

1def tensorflow_control_sources_of_randomness_noncompliant():
2    import tensorflow as tf
3    # Noncompliant: seed is not set.
4    print(tf.random.uniform([1]))

Compliant example

1def tensorflow_control_sources_of_randomness_compliant(seed):
2    import tensorflow as tf
3    # Compliant: sets the seed.
4    tf.random.set_seed(seed)
5    print(tf.random.uniform([1]))