Detailed usage of tf. truncated_normal and tf. random_normal, tf. truncated_normal
This article describes the usage of tf. truncated_normal and tf. random_normal:
Tf. truncated_normal
Copy codeThe Code is as follows:
Tf. truncated_normal (shape, mean = 0.0, stddev = 1.0, dtype = tf. float32, seed = None, name = None)
Output random values from the truncated normal distribution.
The generated value follows the normal distribution with the specified average value and standard deviation. If the generated value is greater than the average value, two standard deviation values are discarded and reselected.
In a normal distribution curve, the area in the horizontal axis interval (μ-σ, μ + σ) is 68.268949%.
The area in the horizontal axis (μ-2 σ, μ + 2 σ) is 95.449974%.
The area in the horizontal axis (μ-3σ, μ + 3σ) is 99.730020%.
The probability that X falls beyond (μ-3σ, μ + 3σ) is less than 3‰. In practice, it is often considered that the corresponding event will not happen, generally, we can regard the interval (μ-3σ, μ + 3σ) as the actual possible value range of random variable X, which is called the "3σ" principle of normal distribution.
In tf. truncated_normal, if the value of x is out of the range (μ-2 σ, μ + 2 σ), select again. This ensures that all generated values are near the mean.
Parameters:
Shape: One-Dimensional Tensor, also the output tensor.
Mean: mean of a normal distribution.
Stddev: Standard deviation of normal distribution.
Dtype: output type.
Seed: an integer. After this parameter is set, the random number generated each time is the same.
Name: Operation name.
import tensorflow as tf; import numpy as np; import matplotlib.pyplot as plt; c = tf.truncated_normal(shape=[10,10], mean=0, stddev=1) with tf.Session() as sess: print sess.run(c)
Output:
[[1.95758033-0.68666345-1.83860338 0.78213859-1.08119416-1.44530308-
0.38035342 0.57904619-0.57145643-1.22899497]
[-0.75853795 0.48202974 1.03464043 1.19210851-0.15739718 0.8506189
1.18259966-0.99061841-0.51968449 1.38996458]
[1.05636907-0.02668529 0.64182931 0.4110294-0.4978295-0.64912242
1.27779591-0.01533993 0.47417602-1.28639436]
[-1.65927458-0.364887-0.45535028 0.078814-0.30295736 1.91779387
-0.66928798-0.14847915 0.91875714 0.61889237]
[-0.01308221-0.38468206 1.34700036 0.64531708 1.15899456
1.22457981-1.1610316 0.59036094-1.97302651]
[-0.24886213 0.82857937 0.09046989 0.39251322-0.21155456
0.18883201 0.08812679-0.32917103 0.20547724]
[0.05388507 0.45474565 0.23398806 1.32670367-0.01957406 0.52013856
-1.13907862-1.71957874 0.75772947-1.01719368]
[0.27155915 0.05900437-0.81448066-0.37997526-0.62020499-0.88820189
1.53407145-0.01600445-0.4236775-1.68852305]
[0.78942037-1.32458341-0.91667277-0.00963761 0.76824385-0.5405798
-0.73307443-1.19854116-0.66179073 0.26329204]
[0.59473759-0.37507254-1.21623695-1.30528259 1.18013096-1.32077384
-0.59241474-0.28063133 0.12341146 0.48480138]
Tf. random_normal
Copy codeThe Code is as follows:
Tf. random_normal (shape, mean = 0.0, stddev = 1.0, dtype = tf. float32, seed = None, name = None)
Output random values from the normal distribution.
Parameters:
- Shape: One-Dimensional Tensor, also the output tensor.
- Mean: mean of a normal distribution.
- Stddev: Standard deviation of normal distribution.
- Dtype: output type.
- Seed: an integer. After this parameter is set, the random number generated each time is the same.
- Name: Operation name.
Code
a = tf.Variable(tf.random_normal([2,2],seed=1))b = tf.Variable(tf.truncated_normal([2,2],seed=2))init = tf.global_variables_initializer()with tf.Session() as sess: sess.run(init) print(sess.run(a)) print(sess.run(b))
Output:
[[-0.81131822 1.48459876]
[0.06532937-2.44270396]
[-0.85811085-0.19662298]
[0.13895047-1.22127688]
After seed is specified, the value of a does not change, and the value of B does not change.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.