Python TensorFlow random uniform

In this Python tutorial, we will learn how to use TensorFlow random uniform() in Python. Also, we will cover the following topics.

  • TensorFlow random normal
  • TensorFlow random normal initializer
  • TensorFlow random_normal seed
  • TensorFlow random uniform initializer
  • TensorFlow has no attribute ‘normal_initializer’
  • TensorFlow random multivariate normal
  • TensorFlow keras random_normal
  • TensorFlow random uniform int

Python TensorFlow random uniform

  • In this section, we will discuss how to use the TensorFlow random.uniform() function in Python.
  • In Python TensorFlow, the random uniform function is used to generate random values and the values will be floating point numbers from a uniform distribution.
  • For example, suppose you have set the range between 2 and 3. By using this method you will get all the interval values between 2 and 3.

Syntax:

Let’s have a look at the Syntax and understand the working of TensorFlow random.uniform() function

tf.random.uniform
                 (
                  shape,
                  minval=0,
                  maxval=None,
                  dtype=tf.dtypes.float32,
                  seed=None,
                  name=None
                 )
  • It consists of a few parameters
    • shape: This parameter indicates the shape of the output tensor.
    • minval: By default it takes 0 value and it specifies the lower bound on the range of random values.
    • maxval: By default it takes None value and it indicates the upper bound on the range of random values.

Example:

import tensorflow as tf

result=tf.random.uniform((3,6),  minval=0,dtype=tf.float32,maxval=2)
print(result)

Here is the Screenshot of the following given code.

Python TensorFlow random uniform
Python TensorFlow random uniform

Also, check: TensorFlow Tensor to numpy

TensorFlow random normal

  • In this section, we will learn how to use the TensorFlow random normal function in Python.
  • In Python, the random normal is used to generate a sample of values from a normal distribution.
  • For example, suppose you have an input tensor of a specified shape and once you will apply this function along with shape then it will return a random value that is actually part of a normal distribution.

Syntax:

Let’s have a look at the syntax and understand the working of TensorFlow random normal function in Python.

tf.random.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 specifies the shape of the return tensor and the input Tensor must be a 1-d integer or you can use the array() function.
    • mean: By default, it takes 0 value and it represents the mean of the distribution and it is an optional argument.
    • stddev: This parameter indicates the standard deviation of the distribution and by default, it takes 1.0 values.
    • dtype: By default it takes tf.dtypes.float32() and if you take input as an integer value then it will return the decimal values in the output.
    • seed: This parameter specifies to declare a random seed for normal distribution and seed is used in generating random numbers.
    • name: By default, it takes none value and it is an optional parameter which defines the name of the operation.

Example:

Let’s take an example and check how to generate the random numbers in a normal distribution.

Source Code:

import tensorflow as tf

result=tf.random.normal((2,4),dtype=tf.float32,seed=4)
print("Tensor normal distriburtion:",result)

Here is the implementation of the following given code.

Python TensorFlow random normal
Python TensorFlow random normal

Read: Import error no module named TensorFlow

TensorFlow random normal initializer

  • In this section, we will discuss how to use the random normal initializer function in Python TensorFlow.
  • To do this task, we are going to use the tf.random_normal_initializer() function and this function is used to return random values that are initialized with random values.

Syntax:

Let’s have a look at the syntax and understand the working of the random normal initializer function in Python.

tf.random_normal_initializer
                            (
                             mean=0.0,
                             stddev=0.05,
                             seed=None
                            )
  • It consists of a few parameters.
    • mean: This parameter specifies the mean of the random values and the input can be scaler tensor and by default it takes 0 values.
    • stddev: This parameter indicates the standard deviation of the random valuesn and by default it takes 0.05 value.
    • seed: In Python the seed is used to declare the random number and this parameter will help the user to create random seeds.

Example:

Let’s take an example and check how to initialize a random number generator with a tensor in Python.

Source Code:

import tensorflow as tf
tf.reset_default_graph()
tf.compat.v1.disable_eager_execution()

new_tensor2 = tf.get_variable('tens',shape=(3,), initializer=tf.random_normal_initializer(seed=0))
with tf.compat.v1.Session() as val:
     val.run(tf.global_variables_initializer())
     print(val.run(new_tensor2))

Here is the execution of the following given code.

TensorFlow random normal initializer in Python
TensorFlow random normal initializer in Python

Read: TensorFlow get shape

TensorFlow random_normal seed

  • In this section, we will discuss how to use the seed parameter in random_normal() function.
  • In Python, the random normal is used to generate a sample of values from a normal distribution and in this example, we have specified the seed parameter.

Syntax:

Here is the Syntax of the random_normal() function in Python TensorFlow.

tf.random.uniform
                 (
                  shape,
                  minval=0,
                  maxval=None,
                  dtype=tf.dtypes.float32,
                  seed=None,
                  name=None
                 )

Note: The seed parameter indicates the creation of a random seed for normal distribution and seed is used in generating random numbers.

Example:

Let’s take an example and check how to generate random values in TensorFlow by using seed parameters.

Source Code:

import tensorflow as tf
tf.reset_default_graph()
tf.compat.v1.disable_eager_execution()

input_tensor = tf.random.normal([3,6], 0, 1, tf.float32, seed=2)
with tf.compat.v1.Session() as val:
     val.run(tf.global_variables_initializer())
     print(val.run(input_tensor))

In the above code we have imported the tensorflow library and then use the tf.compat.v1.disable_eager_execution() function for creating a session. After that we declared a variable ‘input_tensor’ and assign the tf.random.normal() function.

Here is the Screenshot of the following given code.

Python TensorFlow random_normal seed
Python TensorFlow random_normal seed

Read: Module ‘TensorFlow’ has no attribute ‘session’

TensorFlow random uniform initializer

  • In this Program, we will discuss how to use the random uniform initializer function in Python.
  • This function will help the user to generate the input tensor with a uniform distribution.

Syntax:

Let’s have a look at the syntax and understand the working of tf.random_uniform_initializer() function in Python TensorFlow

tf.random_uniform_initializer
                             (
                              minval=-0.05,
                              maxval=0.05,
                              seed=None
                             )
  • It consists of a few parameters
    • minval: This parameter indicates the lower bound of the range of random values and by default it takes negative 0.05 value.
    • maxval: By default it takes positive 0.05 value and it specifies the upper bound of the range of random values.
    • seed: This parameter is used to create a random seeds.

Example:

Let’s take an example and check how to generate a tensor with uniform distribution

Source Code:

import tensorflow as tf
tf.reset_default_graph()
tf.compat.v1.disable_eager_execution()

input_tensor = tf.get_variable('tens',shape=(3,), initializer=tf.random_uniform_initializer(-0.05,0.05,seed=1))
with tf.compat.v1.Session() as val:
     val.run(tf.global_variables_initializer())
     print(val.run(input_tensor))

Here is the execution of the following given code

Python TensorFlow random uniform initializer
Python TensorFlow random uniform initializer

Read: Python TensorFlow expand_dims

TensorFlow has no attribute ‘normal_initializer’

Here we are going to discuss the error Attribute error: TensorFlow has no attribute ‘normal_initializer’ in Python. Basically, this error comes when we have not installed the latest version of TensorFlow.

Reason: The possible reason for this error is that the tf.normal_initailizer is not available in TensorFlow 1. x version.

Example:

import tensorflow as tf
result=tf.random.normal_initializer(shape=[3])
print(result)

Here is the Screenshot of the following given code

TensorFlow has no attribute normal_initializer
TensorFlow has no attribute normal_initializer

As you can see in the Screenshot the output displays the attributeerror module ‘TensorFlow has no attribute ‘normal_initializer.

Here is the solution to this error

Source Code:

import tensorflow as tf
print(tf.__version__)
result=tf.random_normal_initializer(3)
print(result)

Here is the Screenshot of the following given code

Solution of TensorFlow has no attribute normal_initializer
Solution of TensorFlow has no attribute normal_initializer

As you can see in the Screenshot the problem has been solved and we have just simply installed the latest version TensorFlow 2. x because this function is available in the latest version.

To check the version of TensorFlow, you can use the below command

import tensorflow as tf
print(tf.__version__)

Read: Python TensorFlow truncated normal

TensorFlow random multivariate normal

  • In this section, we will discuss how to calculate the multivariate normal distribution in Pyhthon TensorFlow.
  • To perform this particular task we are going to use the tfp.distributions.MultivariateNormalDiag() function and this function will help the user to multivariate the normal distribution.

Syntax:

Let’s have a look at the syntax and understand the working of multivariate Normal functions in Python

tfp.distribution.MultivariateNormalDiag
                                       (
                                        loc=None,
                                        scale_diag=None,
                                       scale_identify_multiplier=None,
                                       validate_args=False,
                                       allow_nan_stats=True,
                                     experimental_use_kahan_sum=False,
                                    )

Example:

import tensorflow as tf
import tensorflow_probability as tfp

tfd = tfp.distributions
new_var = tfd.MultivariateNormalDiag(
    loc=[2., -2],
    scale_diag=[2, 3.])
result=new_var.mean()
print(result)

Here is the Screenshot of the following given code

TensorFlow random multivariate normal in Python
TensorFlow random multivariate normal in Python

Read: Convert list to tensor TensorFlow

TensorFlow keras random_normal

  • In this section, we will discuss how to use the Keras.backend.random_normal() function in TensorFlow Python.
  • To perform this particular task we are going to use the tf.keras.backend.random_normal() function and this method return a tensor with the normal distribution of elements.

Syntax:

Let’s have a look at the syntax and understand the working of tf.Keras.backend.random_normal() function.

tf.keras.backend.random_normal
                              (
                               shape,
                               mean=0.0,
                               stddev=1.0,
                               dtype=None,
                               seed=None,
                              )
  • It consists of a few parameters
    • shape: This parameter indicates the shape of tensor to create.
    • mean: By deafult it takes 0.0 value and it specifies the mean value of the normal distribution.
    • stddev: This parameter indicates the standard deviation of the normal distribution and by default it takes 1.0 value.

Example:

import tensorflow as tf

new_tens = tf.keras.backend.random_normal((3,4),0.0,1.0)
print(new_tens)

Here is the implementation of the following given code.

TensorFlow keras random_normal in Python
TensorFlow Keras random_normal in Python

Read TensorFlow Multiplication

TensorFlow random uniform int

  • In this Program, we will discuss how to use the int datatype in a random uniform function in Python TensorFlow.
  • To do this task, we are going to generate random values and the values will be integer point numbers from a uniform distribution.

Example:

import tensorflow as tf

new_tens=tf.random.uniform((4,8),  minval=0,dtype=tf.int32,maxval=2)
print(new_tens)

In the following given code we have imported the TensorFlow library and then use the tf.random.uniform() function and within this function we have set the dtype=tf.int32 as an argument. Once you will execute this code the output displays the integer random values with the shape of 4 rows and 8 columns.

Here is the Output of the following given code.

TensorFlow random uniform int in Python
TensorFlow random uniform int in Python

In this Python tutorial, we have learned how to use TensorFlow random uniform(). Also, we have covered the following topics.

  • TensorFlow random normal
  • TensorFlow random normal initializer
  • TensorFlow random_normal seed
  • TensorFlow random uniform initializer
  • TensorFlow has no attribute ‘normal_initializer’
  • TensorFlow random multivariate normal
  • TensorFlow keras random_normal
  • TensorFlow random uniform int