Kasparov92 Kasparov92 - 1 month ago 15
Python Question

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]
?

Answer

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

tf.random_uniform(shape, minval=0, maxval=None, dtype=tf.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).
Comments