在 TensorFlow 中生成随机整数
Generating random integers in TensorFlow
我想在 TensorFlow 中生成随机整数,但不知道应该使用哪个命令。特别是,我想从取值 {1, 2, 3, 4} 的统一随机变量生成。我试图查看 tensorflow_probability
中包含的发行版,但没有找到。
在此先感谢您的帮助。
对于均匀分布的简单整数,您可以使用 tf.random.uniform.
为了获得指定的范围和整数,您应该指定 minval
、maxval
和 dtype
参数。所以在你的情况下:
对于 Tensorflow 2.0 及更高版本:
print(tf.random.uniform(shape=(), minval=1, maxval=5, dtype=tf.int32)
对于 Tensorflow 1.15 及以下版本:
with tf.Session() as sess:
random_int = tf.random.uniform(shape=(), minval=1, maxval=5, dtype=tf.int32)
print(sess.run(random_int))
请注意,实际最大值将为 maxval-1
。
我想在 TensorFlow 中生成随机整数,但不知道应该使用哪个命令。特别是,我想从取值 {1, 2, 3, 4} 的统一随机变量生成。我试图查看 tensorflow_probability
中包含的发行版,但没有找到。
在此先感谢您的帮助。
对于均匀分布的简单整数,您可以使用 tf.random.uniform.
为了获得指定的范围和整数,您应该指定 minval
、maxval
和 dtype
参数。所以在你的情况下:
对于 Tensorflow 2.0 及更高版本:
print(tf.random.uniform(shape=(), minval=1, maxval=5, dtype=tf.int32)
对于 Tensorflow 1.15 及以下版本:
with tf.Session() as sess:
random_int = tf.random.uniform(shape=(), minval=1, maxval=5, dtype=tf.int32)
print(sess.run(random_int))
请注意,实际最大值将为 maxval-1
。