Skip to content
Advertisement

How to generate random number in a given range as a Tensorflow variable

I am trying to use the normal distribution to calculate random numbers.

tf.truncated_normal(shape, stddev=0.1,seed=1, mean=0)

but the numbers I get are floating points with many digits after the decimal, like this:0.14845988

Is there a way to make it generate numbers as int, and in a given range like [min, max] ?

Advertisement

Answer

Updated: Tensorflow >= r2.0

The documentation found at https://www.tensorflow.org/guide/random_numbers says that “The old RNGs from TF 1.x such as tf.random.uniform and tf.random.normal are not yet depreciated, but strongly discouraged”, instead, it is recommended to either instantiate a tf.random.Generator object if you want each call to the random number generator to yield different results or using tf.random.stateless_uniform if you are okay with repeated calls on the same machine yielding the same result.

examples from the documentation:

Deterministic and stateful: The generator yields different values on repeated calls. What these values are at the nth call depends on what the seed was when the generator was initialized.

g1 = tf.random.Generator.from_seed(1)
print(g1.normal(shape=[2, 3]))
g2 = tf.random.get_global_generator()
print(g2.normal(shape=[2, 3]))

yields

tf.Tensor(
[[ 0.43842274 -0.53439844 -0.07710262]
 [ 1.5658046  -0.1012345  -0.2744976 ]], shape=(2, 3), dtype=float32)
tf.Tensor(
[[-0.9323887   0.3864468   1.5209497 ]
 [ 0.54473144 -0.6031506  -0.47044003]], shape=(2, 3), dtype=float32)

Non-deterministic and stateful: The generator yields different values on repeated calls, what these values are depends on what the OS and system time were when the generator was initialized.

g = tf.random.Generator.from_non_deterministic_state()
print(g.normal(shape=[2, 3]))

yields

tf.Tensor(
[[-1.3158257   2.4625542   1.3490729 ]
 [ 0.77426016 -2.261468   -0.4887435 ]], shape=(2, 3), dtype=float32)

Stateless: Returns the same value every time. There are several of these types of functions that can be found in the tf.random section of the Tensorflow Core documentation. They all follow the format tf.random.stateless_DISTRIBUTIONNAME. For example:

ints = tf.random.stateless_uniform([10], 
                                   seed = (2,3),
                                   minval=None, 
                                   maxval=None, 
                                   dtype=tf.int)

r1.x >= TensorFlow > r0.11 tf.random.uniform supports minval, maxval and dtypes float32, float64, int32, or int64.

tf.random.uniform(
shape,
minval=0,
maxval=None,
dtype=tf.dtypes.float32,
seed=None,
name=None)

args:

  • shape: A 1-D integer Tensor or Python array. The shape of the output tensor.
  • minval: A 0-D Tensor or Python value of type dtype. The lower bound on the range of random values to generate. Defaults to 0.
  • maxval: A 0-D Tensor or Python value of type dtype. The upper bound on the range of random values to generate. Defaults to 1 if dtype is floating point.
  • dtype: The type of the output: float32, float64, int32, or int64.
  • seed: A Python integer. Used to create a random seed for the distribution. See set_random_seed for behavior.
  • name: A name for the operation (optional).

TensorFlow <= r0.11

tf.random_uniform supports minval, maxval and dtypes float32, float64, int32, or int64.

tf.random_uniform(
    shape, minval=0, maxval=None, dtype=tf.float32, seed=None, name=None)
Advertisement