【bug解决】AttributeError: module ‘tensorflow‘ has no attribute ‘truncated_normal‘

Table of contents

1. Problem description

2. Problem Analysis

3. Further analysis function

Summarize


1. Problem description

If you want to generate normally distributed random numbers in tensorflow, the information found on the Internet is as follows:

import tensorflow as tf

c = tf.truncated_normal(shape=[2, 3], mean=0, stddev=1)

with tf.Session() as sess:
    print(sess.run(c))

# c = tf.compat.v1.random.truncated_normal(shape=[2, 3], mean=0, stddev=1)
# print(c)

But after running, the following error will be reported:

D:\ANACON\envs\L2RPN\python.exe "E:/Undergraduate/Situation Awareness/LeNet-5_learning.py" 
Traceback (most recent call last):
  File "E:\Undergraduate\Situation Awareness\LeNet-5_learning.py", line 3, in <module>
    c = tf.truncated_normal(shape=[2, 3], mean=0, stddev=1)
AttributeError: module 'tensorflow' has no attribute 'truncated_normal'

进程已结束,退出代码1

Right now:


2. Problem Analysis

This is caused by different versions of Tensorflow. I use 2.0. The latest version has changed the function name and usage. details as follows:

import tensorflow as tf

# c = tf.truncated_normal(shape=[2, 3], mean=0, stddev=1)
# with tf.Session() as sess:
#     print(sess.run(c))

c = tf.compat.v1.random.truncated_normal(shape=[2, 3], mean=0, stddev=1)
print(c)

operation result:

tf.Tensor(
[[-0.18886028 -0.737174    1.5540563 ]
 [ 1.5035461   0.23027402 -0.67294544]], shape=(2, 3), dtype=float32)

problem solved


3. Further analysis function

tf.truncated_normal(shape, mean, stddev)

This function produces a normal distribution with a mean and standard deviation that you set yourself. This is a truncated function that generates a normal distribution. The generated values ​​follow a normal distribution with the specified mean and standard deviation. In other words, the generated values ​​are discarded if they differ from the mean by more than two standard deviations. choose. Compared with the general normal distribution to generate random data, the random number generated by this function will not be more than two standard deviations away from the mean, but other functions are generally possible.

In the curve of normal distribution:
the area in the horizontal axis interval (μ-σ, μ+σ) is 68.268949% and the
area in the horizontal axis interval (μ-2σ, μ+2σ) is 95.449974% in
the horizontal axis interval (μ- 3σ, the area within μ+3σ) is 99.730020%

The probability that X falls outside (μ-3σ, μ+3σ) is less than three thousandths. In practical problems, it is often considered that the corresponding event will not happen. Basically, the interval (μ-3σ, μ+3σ) can be viewed as As the actual possible value interval of the random variable X, this is called the "3σ" principle of the normal distribution.
In tf.truncated_normal, if the value of x is outside the interval (μ-2σ, μ+2σ), re-select. This ensures that the generated values ​​are all around the mean.

Interpretation : A truncated random number that generates a normal distribution , that is, if the difference between the random number and the mean is greater than twice the standard deviation, it will be regenerated.

  • shape, the dimension of the generated tensor
  • mean
  • stddev, standard deviation


Summarize

The tensorflow version is different, the function name may be different, and the usage may be slightly different, so pay attention to distinguishing. 

Guess you like

Origin blog.csdn.net/mzy20010420/article/details/127739587