Module ‘tensorflow’ has no attribute ‘truncated_normal’

In this Python tutorial, we will discuss the error “module ‘TensorFlow’ has no attribute ‘truncated_normal’“. And we’ll also cover the following topics:

  • Attributeerror: module ‘tensorflow’ has no attribute ‘truncated_normal’
  • Attributeerror: module ‘tensorflow’ has no attribute ‘truncated_normal_initializer’

Also, check the latest tutorial on TensorFlow: Module ‘tensorflow’ has no attribute ‘optimizers’

Attributeerror: module ‘tensorflow’ has no attribute ‘truncated_normal’

  • In this section, we will discuss how to solve the error module ‘tensorflow’ has no attribute ‘truncated_normal’.
  • To perform this particular task we are going to use the truncated_normal Any samples that deviate more than two standard deviations from the mean are discarded and reduced, and the values are taken from a normal distribution with a known mean and standard deviation, and this method is used to generate random values from a normal distribution and normal distribution means probability distribution that occurs in many events.

Example:

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)

Here is the Screenshot of the following given code

ule 'tensorflow' has no attribute 'truncated_normal'
module ‘tensorflow’ has no attribute ‘truncated_normal’

The solution to this error

In this example, we will use the tf.random.truncated_normal() function and this function return a output random values from a truncated normal distribution.

Syntax:

Here is the Syntax 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 defines a Python or Tensor array of 1-D integers. The output tensor’s form.
    • mean: By default, it takes a 0.0 value and a Python value 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 it specifies the data type of the tensor.
    • seed: an integer in Python, and it is used to produce the distribution’s random seed.
    • name: It defines the name of the operation and by default, it takes none value.
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)

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 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, and stddev as an argument. Once you will execute this code the output displays random values from a normal distribution.

You can refer to the below Screenshot.

Solution of attributeerror: module 'tensorflow' has no attribute 'truncated_normal'
Solution of attributeerror: module ‘tensorflow’ has no attribute ‘truncated_normal’

This is how we can solve the attribute error module ‘tensorflow’ has no attribute ‘truncated_normal’.

Read: Module ‘tensorflow’ has no attribute ‘log’

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

tensorflow has no attribute truncated_normal_initializer
tensorflow has no attribute truncated_normal_initializer

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

solution of attributeerror module tensorflow has no attribute truncated_normal_initializer
solution of attributeerror module tensorflow has no attribute truncated_normal_initializer

As you can see in the Screenshot we have solved the attributeerror module tensorflow has no attribute truncated_normal_initializer.

You may also like to read the following TensorFlow tutorials.

In this Python tutorial, we have discussed the error “module ‘TensorFlow’ has no attribute ‘truncated_normal’“. And we have also covered the following topics:

  • Attributeerror: module ‘tensorflow’ has no attribute ‘truncated_normal’
  • Attributeerror: module ‘tensorflow’ has no attribute ‘truncated_normal_initializer’