随机初始化值randint,rand,tf.random_normal,tf.random_uniform

import tensorflow as tf

import numpy as np

原型:np.random.randint(low, high=None, size=None, dtype='l')
'''
  当只有low时候,返回的值范围是[0,low).有low和high时候,返回值范围是[low,high).
'''
t1 = np.random.randint(2,size=10)
print(t1)
#[0 0 0 0 1 0 1 1 1 1]

t2 = np.random.randint(low=1,high=3,size=10)
print(t2)
#[2 1 2 1 2 2 2 1 1 2]
原型:np.random.rand(d0, d1, ..., dn),其中di表示维数
'''
    返回范围为[0,1)均匀分布
'''
t3 = np.random.rand(3, 2)
print(t3)
#[[0.25586789 0.26593995]
# [0.00827676 0.67958833]
# [0.77343696 0.40320088]]

原型:tf.random_normal(shape,
   mean=0.0,
   stddev=1.0,
   dtype=dtypes.float32,
   seed=None,
   name=None)
'''
    根据shape返回一个张量,其中值服从均值为0,方差为1的正态分布
'''
t4 = tf.random_normal((3, 2))
print(t4)
#Tensor("random_normal:0", shape=(3, 2), dtype=float32)

with tf.Session() as sess:
    init =tf.global_variables_initializer()
    print(sess.run(t4))
#[[-0.1009187  -0.52692866]
# [ 0.75775075  0.10555366]
# [ 0.89376223 -1.5488473 ]]
原型:tf.random_uniform(shape,
   minval=0,
   maxval=None,
   dtype=dtypes.float32,
   seed=None,
   name=None)
'''
    从均匀分布中随机取值,范围为[minval,maxval)
'''
t5 = tf.random_uniform((3, 2),minval=1,maxval=3)
print(t5)
#Tensor("random_uniform:0", shape=(3, 2), dtype=float32)

with tf.Session() as sess:
    init =tf.global_variables_initializer()
    print(sess.run(t5))
#[[2.8821492 1.3117931]
# [2.6424809 1.5386689]
# [1.4922662 1.0668414]]


非学无以广才,非志无以成学! 【Magic_chao

原文地址:https://www.cnblogs.com/logo-88/p/9087768.html