In this tutorial, I will show you how to solve the error **Attributeerror: Module ‘tensorflow’ has no attribute ‘truncated_normal_initializer’**.

When I updated the TensorFlow version to 2.x, this error began to show in my terminal; I did the research and found the solution, so here I have explained three methods that you can use to prevent this kind of error.

Let’s begin,

## Attributeerror: Module ‘tensorflow’ has no attribute ‘truncated_normal_initializer’

This error **Attributeerror: Module ‘tensorflow’ has no attribute ‘truncated_normal_initializer’**, which means there is no attribute **‘truncated_normal_initialzer’** in the TensorFlow library.

First, let me briefly explain the **truncated_normal_initializer** attribute,

**truncated_normal_initializer()**function, and this function initializer generates a truncated normal distribution.- The only difference between these values and those from a random normal initializer is that values deviate by more than two standard deviations are discarded and reduced. The weights and filters of neural networks should be initialized using this method.

So now the below code is the root cause of this error.

```
import tensorflow as tf
new_trunc = tf.truncated_normal_initializer(mean=14,stddev=1,seed=4)
print(new_trunc)
```

Look error appears when you try using the TensorFlow’s **truncated_normal_initializer()** attribute. The reason is the version of TensorFlow that you are using and the code compatibility of the TensorFlow version.

Recently, when TensorFlow was upgraded to version 2.x, many changes were made to this new version, including how to access initializers.

So there are three ways to solve this error:

- First, use the
**truncated_normal_initializer()**by accessing it through**TensorFlow version 1 mode**in the environment of TensorFlow version 2. - The second solution is to use the submodule
**tf.initializers**, and the third is to use the other submodule, which is**tf.keras.initializers**.

Let’s begin with Tensorflow version 1.x mode. In the TensorFlow version 1.x , you can directly access the truncated_normal_initializer() attribute. But you have installed version 2.x; how will you access it?

TensorFlow provided a solution for that. If you want to access the function or attributes of version 1.x into the TensorFlow version 2.x, the solution is to use **truncated_normal_initializer** from the module **tf. compat.v1**.

Now rerun the above code as shown below.

```
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
new_trunc = tf.compat.v1.truncated_normal_initializer(mean=14,stddev=1,seed=4)
print(new_trunc)
```

From the output, successfully access the **truncated_normal_initializer()** attribute from the module **tf.compat.v1** in the environment of Tensorflow version 2.x.

The next solution is to use the **tf.initializers** submodule of TensorFlow 2, so access the attribute **TruncatedNormal()** using tf.initializers as shown in the code below.

```
import tensorflow as tf
new_trunc = tf.initializers.TruncatedNormal(mean=14,stddev=1,seed=4)
print(new_trunc)
```

In the above code, the **TruncatedNormal()** attribute is used with **mean=14, stddev=1 and seed=4** from the submodule **tf.initializers** of TensorFlow.

Lastly, you can use the **tf.keras.initializers** submodule, now use the above code again but access the TruncatedNormal() attribute from the tf.keras.initializers as shown in below.

```
import tensorflow as tf
new_trunc = tf.keras.initializers.TruncatedNormal(mean=14,stddev=1,seed=4)
print(new_trunc)
```

Again, initialize the variable **new_trunc** using the TruncatedNormal from the submodule **tf.keras.initialzers**.

So this is how to fix the error **Attributeerror: Module ‘tensorflow’ has no attribute ‘truncated_normal_initializer’** without getting any error.

If you want to know more about using the truncated_normal() attribute, visit TensorFlow documentation tf.compat.v1.truncated_normal_initializer.

## Conclusion

You learned how to fix the error **Attributeerror: Module ‘tensorflow’ has no attribute ‘truncated_normal_initializer’**.

You learned three methods that solve that error. First, you used the **tf. compat.v1** module to access the **truncated_normal_initializer** in the TensorFlow version 2 environment.

After that, to achieve the same functionality, you accessed the same attribute from the two submodules in TensorFlow, **tf.initializers** and **tf.keras.initializers**.

You may like to read:

- Attributeerror: Module ‘tensorflow’ has no attribute ‘trainable_variables’
- Module ‘tensorflow’ has no attribute ‘truncated_normal’
- Module ‘tensorflow’ has no attribute ‘get_variable’

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.