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.
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.
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 :
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.
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.