tf.random_uniform()简介
tf.random_uniform(
shape,
minval=0,
maxval=None,
dtype=tf.float32,
seed=None,
name=None
)
tf.random_uniform([4,4], minval=-10,maxval=10,dtype=tf.float32)))返回4*4的矩阵,产生于-10和10之间的数,产生的值是均匀分布的。
实例
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
# 实例1:session 保留了随机数的状态
c = tf.random_uniform([], -10, 10, seed=2)
with tf.Session() as sess:
print(sess.run(c)) # >>3.574932
print(sess.run(c)) # >>-5.9731865
# 实例2:每一个新的session将会重新还原随机数的状态
c = tf.random_uniform([], -10, 10, seed=2)
with tf.Session() as sess:
print(sess.run(c)) # >> 3.574932
with tf.Session() as sess:
print(sess.run(c)) # >> 3.574932
# 实例3 在计算级别中设置相同的seed,将会产生相同的随机数
c = tf.random_uniform([], -10, 10, seed=2)
d = tf.random_uniform([], -10, 10, seed=2)
with tf.Session() as sess:
print(sess.run(c)) # >> 3.574932
print(sess.run(d)) # >> 3.574932
# 实例4 在计算级别中设置不同的seed,将会产生不同的随机数
c = tf.random_uniform([], -10, 10, seed=1)
d = tf.random_uniform([], -10, 10, seed=2)
with tf.Session() as sess:
print(sess.run(c)) # >> -5.219252
print(sess.run(d)) # >> 3.574932
# 实例 5 在图级别设置随机种子,将会产生不同的数
tf.set_random_seed(2)
c = tf.random_uniform([], -10, 10)
d = tf.random_uniform([], -10, 10)
with tf.Session() as sess:
print(sess.run(c)) # >> 9.123926
print(sess.run(d)) # >> -4.5340395
网友评论