In this Tensorflow tutorial, we will see, how to fix the error, “**attributeerror module ‘tensorflow’ has no attribute ‘squared_difference’**“. The error I got while working with squared_difference in TensorFlow.

- Attributeerror: module ‘tensorflow’ has no attribute ‘squared_difference’
- Attributeerror: module ‘tensorflow’ has no attribute ‘truncated_normal_initializer’

## Attributeerror: module ‘tensorflow’ has no attribute ‘squared_difference’

- Here we will discuss how to solve the attributeerror: module ‘tensorflow’ has no attribute
**‘squared_difference’**. - To do this task first we will import the TensorFlow library with tf alias where tf represents the TensorFlow and is used for numerical computation problems. Next, we will create a variable in the form of tensors and assign a
**tf.constant()**function. In Python, this function takes a constant value that represents the value that does not modify and also initializes an object like an array or list. - In Tensorflow the squared difference is used to calculate the element-wise
**(x-y)(x-y)**.

**Syntax**:

Let’s have a look at the Syntax and understand the working of **tf.math.squared_difference()** function in Python TensorFlow.

```
tf.math.squared_difference(
x,
y,
name=None
)
```

- It consists of a few parameters
**x**: This parameter defines the input tensor and it must be one of the following types: bfloat16, half, float32, float64, int32, int64, complex64, and complex128.**y**: This parameter specifies the second input tensor and it must be of the same type as x.**name**: By default, it takes none value and specifies the name of the operation.

**Example**:

```
import tensorflow as tf
Population_of_USA = tf.constant([ 782, -745, 290, 512, 756], dtype = tf.float64)
tensor = tf.constant([ 654, 278, 156, 945, 345], dtype = tf.float64)
# Calculating result
new_output = tf.squared_difference(Population_of_USA, tensor)
# Printing the result
print('Result: ', new_output)
```

Here is the Screenshot of the following given code

As you can see in the Screenshot the output displays the error** AttributeError: module ‘TensorFlow’** has no attribute ‘squared_difference’.

**Reason: **The possible reason for this error is that the tf.squared_difference attribute is not available in Tensorflow’s latest version (TensorFlow2.0).

Now let’s see the solution to this

### Solution

```
import tensorflow as tf
Population_of_USA = tf.constant([ 782, -745, 290, 512, 756], dtype = tf.float64)
tensor = tf.constant([ 654, 278, 156, 945, 345], dtype = tf.float64)
# Calculating result
new_output = tf.math.squared_difference(Population_of_USA, tensor)
# Printing the result
print('Result: ', new_output)
```

In the above-given code, we have created a tensor by using the** tf.constant() **function, and the name of the tensor is** ‘population_of_USA’**. Now we want to calculate the input tensor values by using the** tf.math.squared_diffference() **function.

Here is the implementation of the following given code

This is how to fix the error, attributeerror module ‘tensorflow’ has no attribute ‘squared_difference’.

Read TensorFlow Learning Rate Scheduler

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

- Let us discuss how to solve the error module ‘tensorflow’ has no attribute ‘truncated_normal_initializer’.
- To perform this particular task we are going to use the
**tf.compat.v1.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 that deviate by more than two standard deviations are discarded and reduced. The weights and filters of neural networks should be initialized using this method.

**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**:

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

Here is the Screenshot of the following given code

### Solution

The solution to this error

```
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)
```

You can refer to the below Screenshot

This is how to fix the error, Attributeerror: module ‘tensorflow’ has no attribute ‘truncated_normal_initializer’.

In this article, we have discussed how to solve the error **attributeerror module ‘tensorflow’ has no attribute ‘squared_difference’**. Let us discuss how to use the squared_difference in TensorFlow. And we have covered the following topics:

- Attributeerror: module ‘tensorflow’ has no attribute ‘squared_difference’
- Attributeerror: module ‘tensorflow’ has no attribute ‘truncated_normal_initializer’

You may like the following Tensorflow tutorials:

- Python program for current date and time
- Module ‘tensorflow’ has no attribute ‘optimizers’
- Tensorflow convert sparse tensor to tensor
- Module ‘tensorflow’ has no attribute ‘div’
- Module ‘tensorflow’ has no attribute ‘sparse_placeholder’
- 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.