What is difference between tf.truncated_normal and tf.random_normal?

前端 未结 3 2040
轻奢々
轻奢々 2020-12-13 08:44

tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None) outputs random values from a normal distribution.

tf.truncat

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 09:00

    The documentation says it all: For truncated normal distribution:

    The generated values follow a normal distribution with specified mean and standard deviation, except that values whose magnitude is more than 2 standard deviations from the mean are dropped and re-picked.

    Most probably it is easy to understand the difference by plotting the graph for yourself (%magic is because I use jupyter notebook):

    import tensorflow as tf
    import matplotlib.pyplot as plt
    
    %matplotlib inline  
    
    n = 500000
    A = tf.truncated_normal((n,))
    B = tf.random_normal((n,))
    with tf.Session() as sess:
        a, b = sess.run([A, B])
    

    And now

    plt.hist(a, 100, (-4.2, 4.2));
    plt.hist(b, 100, (-4.2, 4.2));
    


    The point for using truncated normal is to overcome saturation of tome functions like sigmoid (where if the value is too big/small, the neuron stops learning).

提交回复
热议问题