In this TensorFlow tutorial, I will demonstrate how to resolve the error **Attributeerror: Module ‘tensorflow’ has no attribute ‘truncated_normal’**.

I used the **truncated_normal()** attribute in my project to compute the truncated normal distribution; when I ran my project, this error showed up. After researching and debugging, I found the solution.

So here, I will share that solution with you, which will be helpful if you get the same error.

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

The error **Attributeerror: Module ‘tensorflow’ has no attribute ‘truncated_normal’**, meaning you want to access the attribute **truncated_normal()** from the Tensorflow, but this attribute doesn’t belong to the TensorFlow.

When running the code below, this error began to appear.

```
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
input_tens = tf.constant(50,dtype="int32",name="input_tensor")
result=tf.truncated_normal(shape=[input_tens], mean=4, stddev=1)
with tf.compat.v1.Session() as val:
new_output=val.run(result)
print(new_output)
```

When you execute the above code, you get the error. The reason for this error is the changes in the latest version of Tensorflow.

This means your code works fine with Tensorflow version 1.x, but with version 2.x, it shows that error. So you have updated your tensorflow but are using the code compatible with the old version.

This error has two solutions:

- First, use
**tensorflow.random**submodule to access the**truncated_normal()**attribute. - Second, use the
**tf.compat.v1**module allows access to the function or module of Tensorflow version 1.x into the current environment of TensorFlow version 2.x.

Let’s start with **tensorflow.random**, so I have shown complete syntax for using the **truncated_normal() **attribute.

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

Where parameters are:

**shape**: This parameter defines a Python or Tensor array of 1-D integers. The output tensor’s form.**mean**: By default, it takes a 0.0 and Python values of type dtype or a 0-D tensor. the average of a normal distribution that was shortened.**stddev**: a Python value of type dtype or a 0-D tensor and the initial standard deviation of the normal distribution.**dtype**: By default, it takes tf.dtypes.float32 and specifies the tensor’s data type.**seed**: an integer in Python that produces the distribution’s random seed.**name**: It defines the name of the operation, and by default, it takes none value.

Now access the **truncated_normal()** function from the **tf.compat.v1** module and rerun the above code.

```
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
input_tens = tf.constant(50,dtype="int32",name="input_tensor")
result=tf.random.truncated_normal(shape=[input_tens], mean=4, stddev=1)
with tf.compat.v1.Session() as val:
new_output=val.run(result)
print(new_output)
```

Look at the output of the above code; we have imported the Tensorflow library and then used the **tf. compact.v1.disable_eager_execution** function for creating the session and disabling the eager execution.

After that, apply the constant function for creating a tensor shape and use the **tf.random.truncated_normal()** function, and within this function, we have assigned the shape, mean, and stddev as an argument. Once you execute this code, the output displays random values from a normal distribution.

Now move to a second solution, where you will access the **truncated_normal()** attribute from the **tf.compat.v1** module.

```
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
input_tens = tf.constant(50,dtype="int32",name="input_tensor")
result=tf.compat.v1.truncated_normal(shape=[input_tens], mean=4, stddev=1)
with tf.compat.v1.Session() as val:
new_output=val.run(result)
print(new_output)
```

Look here; the **truncated_normal** is accessed from the **tf.compat.v1** submodule to create a truncated normal distribution. But I suggest you go with the first solution because this solution is depreciated.

This is how to resolve the error Attributeerror: Module ‘tensorflow’ has no attribute ‘truncated_normal’.

## Conclusion

In this TensorFlow tutorial, you learned how to fix the error **Attributeerror: Module ‘tensorflow’ has no attribute ‘truncated_normal’**.

You used two methods to solve this error; in the first method, you used the tf.random submodule of the TensorFlow version 2.x, and in the second, you used the tf. compact.v1 module.

You may like to read:

- Attributeerror: Module ‘tensorflow’ has no attribute ‘variable_scope’
- Attributeerror: Module ‘keras.optimizers’ has no attribute ‘sgd’
- 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.