Attributeerror: module ‘tensorflow’ has no attribute ‘random_normal’

Attributeerror_ module 'tensorflow' has no attribute 'random_normal'

Attributeerror: module ‘tensorflow’ has no attribute ‘random_normal’ error occurs because of using random_normal in the place of random.normal in TensorFlow 2. x.  While training any deep learning model, we need to initialize normal distribution weight in different layers. To achieve this we use random. normal in TensorFlow 2. x version. Some how if we use random_normal , It creates a syntax Attributeerror. In this article, we will explore the use of random.normal to fix this error.

attributeerror: module ‘tensorflow’ has no attribute ‘random_normal’ ( Solution ) –

There are two ways to fix this error.

Solution 1: Syntax fix by using random.normal –

Use the below syntax to fix this error.

tf.random.normal(
    shape,
    mean=0.0,
    stddev=1.0,
    dtype=tf.dtypes.float32,
    seed=None,
    name=None
)

Actually, the older version of Tensorflow has the alias ‘random_normal’ for this. But some after the release 2.1.0 it is random.normal is the correct syntax. Actually, the reason behind this change is most of the random distributions were already in random tensorflow.random except this normal.

attributeerror module 'tensorflow' has no attribute 'random_normal'
attributeerror module ‘tensorflow’ has no attribute ‘random_normal’

 

Solution 2: Downgrading the tensorflow version –

Since the random_normal version is supportive and works in a lower version of TensorFlow. The command which we can use to install the lower version of TensorFlow.

pip install tensorflow==1.13.2

Here we used version 1.32.2 but you can use any TensorFlow version which is compatible with random_normal syntax.  Also in the place of pip, you can use a package manager as well like conda, easy_install, etc. Hope now you can fix up this error.

Related Errors :

1.Attributeerror: tensor object has no attribute numpy : Tricks to Fix

2.attributeerror: module tensorflow has no attribute contrib ( Solved )

If you want to understand more about Attributeerrors in Python, read the below article. It will give the general ways to fix the attributeerror. Ultimately the underline packages will change and accordingly the related commands will also change but the root of the solution will be common.

What is AttributeError in Python ? Complete Overview

Thanks
Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
Share via
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner