Stateless Random Functions

In TensorFlow, Stateless Random Number Generators (RNGs) are the functions that generate random numbers without maintaining any internal state. They are purely functional as they produce the same output for the same set of input arguments.

Stateless random number generation is achieved using cryptographic hash functions to generate random numbers based on input seeds. This ensures that the same input seeds will always produce the same random numbers, making the process deterministic and reproducible.

The tf.random.stateless module provides functionality for stateless random number generation.

Stateless RNGs are deterministic which makes them useful in cases where we need reproducible results are required. Check the code given below, where we have generated random uniform numbers using tf.random.stateless_uniform function.

  • After importing TensorFlow library, we have defined a Stateless RNG Key with seed values. These seed values are used as input to the stateless RNG to produce random numbers.
  • Then, we generate random uniform numbers using the Stateless RNG.

Python3




import tensorflow as tf
 
# Define a stateless RNG key
key = tf.constant([1, 2], dtype=tf.int32)
 
# Generate random uniform numbers using the stateless RNG
random_uniform_numbers = tf.random.stateless_uniform(shape=(4, 2), seed=key)
 
print("Random Uniform Numbers:")
print(random_uniform_numbers)


Output:

Random Uniform Numbers:
tf.Tensor(
[[0.8440604 0.19204533]
[0.9962232 0.1603874 ]
[0.27199018 0.649346 ]
[0.10694444 0.06516242]], shape=(4, 2), dtype=float32)

In the above code, tf.random.stateless_uniform function is used to generate a 4×2 tensor of random uniform numbers. The function takes as an input, a shape argument and a stateless RNG key (seed). If you use the same key, then the same set of random numbers will be produced always.

Deterministic Behavior

Stateless RNGs maintain deterministic behavior based on their input arguments. If you re-run the same code with same input arguments, you will get identical random numbers.

Python3




# Re-run with the same key
random_uniform_numbers_again = tf.random.stateless_uniform(shape=(4, 2), seed=key)
 
print("Random Uniform Numbers (Again):")
print(random_uniform_numbers_again)


Output:

Random Uniform Numbers (Again):
tf.Tensor(
[[0.8440604 0.19204533]
[0.9962232 0.1603874 ]
[0.27199018 0.649346 ]
[0.10694444 0.06516242]], shape=(4, 2), dtype=float32)

You can see that we got the same numbers as we used the code with the same seed (key).

Random number generation using TensorFlow

In the field of Machine Learning, Random numbers generation plays an important role by providing stochasticity essential for model training, initialization, and augmentation. We have TensorFlow, a powerful open-source machine learning library, that contains tf.random module. This module helps us for implementing reproducible random number generation.

In this article, we will understand how we can generate random numbers by using the tools provided by TensorFlow. Two major approaches that we will discuss are:

  1. tf.random.Generator Class
  2. Stateless Random Functions

Let us discuss each of these approaches in detail.

Similar Reads

tf.random.Generator Class

TensorFlow provides us with tf.random.Generator which is a powerful tool for managing random number generation. It allows us to create unique random numbers, each of them having their own internal state. This internal state is updated each time a random number is generated. It provides a level of control and reproducibility....

Stateless Random Functions

...

Difference Between Stateless RNGs and tf.random.Generator

...

Conclusion

...

Contact Us