Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating random integers in TensorFlow

I would like to generate random integers in TensorFlow but I don't which command I should use. In particular, I would like to generate from a uniform random variable which takes values in {1, 2, 3, 4}. I have tried to look among the distributions included in tensorflow_probability but I didn't find it.

Thanks in advance for your help.

like image 288
Maurizio Serra Avatar asked Jun 15 '26 15:06

Maurizio Serra


1 Answers

For simple integers from uniform distribution you could use tf.random.uniform.
In order to get the specified range and integers you should specify the minval, maxval and dtype parameters. So in your case:

For Tensorflow 2.0 and above:

print(tf.random.uniform(shape=(), minval=1, maxval=5, dtype=tf.int32)

For Tensorflow 1.15 and below:

with tf.Session() as sess:
    random_int = tf.random.uniform(shape=(), minval=1, maxval=5, dtype=tf.int32)
    print(sess.run(random_int))

Note that the actual maximum value will be maxval-1.

like image 58
sebastian-sz Avatar answered Jun 17 '26 23:06

sebastian-sz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!