In this Python tutorial, we will learn** how to use the truncated normal **function in TensorFlow. Additionally, we will cover the following topics.

- TensorFlow distribution truncated_normal
- TensorFlow truncated_normal_initailizer
- TensorFlow truncated normal example
- TensorFlow has no attribute ‘truncated_normal’
- TensorFlow has no attribute ‘truncated_normal_initializer’

## Python TensorFlow Truncated normal

- In this section, we will discuss how to use the truncated normal function in Python TensorFlow.
- To perform this particular task, we are going to use the
**tf.random.truncated_normal()**function and this method is used to generate random values from a normal distribution and normal distribution means probability distribution that occurs in many events. - In this example, we are going to use a normal constant variable that will consider as the shape of the tensor and apply in
**tf.random.truncated_normal()**function.

**Syntax:**

Let’s have a look at the Syntax and understand the working of **tf.random.truncated_normal()** function.

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

- It consists of a few parameters
**shape:**This parameter indicates the output of the given tensor and it must be a one-dimensional Tensor that contains an only integer value.**mean:**By default it takes**0**value and it specifies that we have to find the mean value of the given normal distribution.**stddev:**This parameter indicates that we have to find the standard deviation of the given normal distribution and by default, it takes a**1.0**value.**dtype:**By default it takes**tf.dtypes.float32()**value and it indicates the datatype of output.**name:**This is an optional parameter and defines the name of the operation(truncate name).

**Example:**

Let’s take an example and check how to use the **tf.random.truncated_normal()** function.

**Source Code:**

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

In the above code, we have imported the Tensorflow library and then use the **tf.compat.v1.disable_eager_execution** function for creating the session.

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

Here is the Screenshot of the following given code.

Read: TensorFlow Tensor to NumPy

## TensorFlow distribution truncated_normal

- Here we are going to discuss how to use the
**distribution truncated_normal()**function in Python TensorFlow. - In Python, this function is a normal distribution and it is in the limit between low and high parameters while the probability density is
**0**outside these limits.

**Syntax:**

Let’s have a look at the Syntax and understand the working of **tfp.distribution.TruncatedNormal()** function in Python TensorFlow.

```
tfp.distribution.TruncatedNormal
(
loc,
scale,
low,
high,
validate_args=False,
allow_nan_stats=True,
name='TruncatedNormal'
)
```

- It consists of a few parameters
**loc:**This parameter indicates the mean of the normal distribution and the value must be a float.**scale:**This parameter specifies the standard deviation of the normal distribution.**low:**It is a lower limit of the normal distribution and represents floating values.**high:**It is the upper limit of the normal distribution and indicates floating values.**validate_args:**By default, it takes a**false**value and if it is true then distribution arguments are checked at run time.**allow_nan_stats:**By default, it takes a**true**value and if it is**‘false’**an exception is raised if the statistics nan values are undefined.

**Example:**

Let’s take an example and check how to use the **distribution truncated_normal() function** in Python.

**Source Code:**

```
import tensorflow as tf
import tensorflow_probability as tfp
tf.compat.v1.disable_eager_execution()
new_trunc = tfp.distributions.TruncatedNormal(loc=[1., 2.], scale=2.,
low=[-2., 1.],
high=[2., 2.])
result=new_trunc.prob([0.6, 0.9])
with tf.compat.v1.Session() as val:
new_output=val.run(result)
print(new_output)
```

In the following given code, we have imported the **tensorflow_probability()** library with the alias name ‘tfp’.

After that, we have used the **tfp.distribution.TruncatedNormal()** function and within this function, we have assigned** loc, scale, low, and high** as an argument.

Here is the implementation of the following given code

Read: TensorFlow get shape

## TensorFlow truncated_normal_initailizer

- In this Program, we will discuss how to use the
**truncated_normal_initializer()**function in TensorFlow Python. - In Python, the initializer means that we have to generate tensors with a normal distribution.

**Syntax:**

Here is the Syntax of **tf.compat.v1.truncated_normal_initializer()** function in Python TensorFlow.

```
tf.compat.v1.truncated_normal_initializer
(
mean=0.0,
stddev=1.0,
seed=None,
dtype=tf.dtypes.float32
)
```

**Example:**

Let’s take an example and check how to use the** truncated_normal_initializer()** function in Python.

**Source Code:**

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

You can refer to the below Screenshot.

Read: Python TensorFlow reduce_sum

## TensorFlow truncated normal example

- In this example, we will discuss how to generate the random values from the truncated normal distribution in TensorFlow Python.
- To perform this particular task, we are going to use the
**tf.random.truncated_normal()**function and this function is used to generate the random values from a truncate normal distribution.

**Syntax:**

Here is the Syntax of** tf.random.truncated_normal()** function in Python TensorFlow.

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

**Example:**

```
import tensorflow as tf
trunc_tens=tf.random.truncated_normal([4])
print(trunc_tens)
```

In the above code, we have imported the Tensorflow library and then used the **tf.random.truncated_normal()** function and within this function, we have assigned the shape of the output tensor.

Here is the execution of the following given code.

Read: Python TensorFlow reduce_mean

## TensorFlow has no attribute ‘truncated_normal’

Here we are going to discuss the error Attribute error: **module ‘TensorFlow’ has no attribute ‘truncated_normal’** in Python. Basically, this error statement comes when we used the **tf.truncated_normal()** function.

**Example:**

```
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
tens = tf.constant(10)
result=tf.truncated_normal(shape=[tens], mean=3, stddev=1)
with tf.compat.v1.Session() as val:
new_output=val.run(result)
print(new_output)
```

Here is the Screenshot of the following given code

**Now let’s see the solution to this error**

```
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
tens = tf.constant(10)
result=tf.random.truncated_normal(shape=[tens], mean=3, stddev=1)
with tf.compat.v1.Session() as val:
new_output=val.run(result)
print(new_output)
```

In the above code, we have used the **tf.random.truncated_normal()** function instead of t**f.truncated_normal()** function. In Python the **tf.random.truncated_normal()** function is used to generate the random values from normal truncated distribution.

Here is the Output of the following given code

Read: Import error no module named TensorFlow

## TensorFlow has no attribute ‘truncated_normal_initializer’

In this section, we will discuss the error Attribute error module **‘TensorFlow’ has no attribute ‘truncated_normal_initializer**‘ in Python. Basically, this error statement comes when we used the **tf.truncated_normal_initializer()** function.

**Example:**

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

Here is the implementation of the following given code

**Now let’s see the solution to this error**

To get the solution to this error, you can refer to our previous topic **TensorFlow truncated_normal_initailizer**.

In Python TensorFlow, the latest version** 2.8** has been updated most of the functions. If you are using the latest version of TensorFlow then you can apply the **tf.compat.v1.truncated_normal_initializer()** function to solve this error.

Also, check the following TensorFlow tutorials:

- Tensorflow iterate over tensor
- TensorFlow Graph – Detailed Guide
- TensorFlow Sparse Tensor + Examples
- Module ‘TensorFlow’ has no attribute ‘session’
- Module ‘TensorFlow’ has no attribute ‘get_default_graph’

In this Python tutorial, we have learned** how to use the truncated normal **function in Python. Additionally, we have covered the following topics.

- TensorFlow distribution truncated_normal
- TensorFlow truncated_normal_initailizer
- TensorFlow truncated normal example
- TensorFlow has no attribute ‘truncated_normal’
- TensorFlow has no attribute ‘truncated_normal_initializer’

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.