TensorFlow Get Variable + Examples

In this Python tutorial, we will learn how to get the variable in Python TensorFlow. Also, we will cover the following topics.

  • TensorFlow get variable by name
  • TensorFlow get variable value
  • TensorFlow get variable scope
  • TensorFlow get variable initializer
  • TensorFlow get variable list

TensorFlow get variable

  • In this section, we will discuss how to get the variable in Python TensorFlow.
  • To perform this particular task, we are going to use the tf.compat.v1.get_variable() function and this function is used to get the given variable with these arguments.
  • In this example we will create a simple variable ‘tens’ and assign the tf.compat.v1.get_variable() function, and within this function we are going to assign the name and dtype parameter to it.

Syntax:

Let’s have a look at the Syntax and understand the working of the tf.compat.v1.get_variable() function in Python TensorFlow.

tf.compat.v1.get_variable
                         (
                          name,
                          shape=None,
                          dtype=None,
                          initializer=None,
                          regularizer=None,
                          trainable=None,
                          collections=None,
                          caching_devices=None,
                          partitioner=None,
                          validate_shape=true,
                          use_resource=None,
                          custom_getter=None,
                          constraint=None,
                          synchronization=tf.variableSynchronization.AUTO,
                          aggregation=tf.compat.v1.variableAggregation.None
                         )
  • It consists of a few parameters
    • name: This parameter indicates the name of the given variable.
    • shape: This parameter defines the shape of the given or existing variable.
    • dtype: By default it takes tf.float32() value and it exists in a given variable.
    • initializer: By default, it takes none value and it specifies whether it can be a tenor or initializer.
    • regularizer: This parameter is used for regularization.
    • trainable: By default, it takes none value and it will check the condition if it is true then it will add the given variable to the graph collection.
    • collections: It defines the list of graph collections and by default, it takes None value.

Example:

Let’s take an example and check how to get the variable in Python TensorFlow.

Source Code:

import tensorflow as tf
tens = tf.compat.v1.get_variable(name='tens',shape=[1],dtype=tf.int32)
print(tens)

In the following code, we have imported the TensorFlow library and then we have created a variable in which we have assigned the tf.compat.v1.get_variable() function. And within this function, we have passed name, shape, and datatype as an argument. Once you will execute this code the output displays the name of the variable.

Here is the Screenshot of the following given code.

TensorFlow get variable in Python
TensorFlow get variable in Python

Read: TensorFlow Tensor to NumPy

TensorFlow get variable by name

  • In this Program, we will discuss how to get the variable by name in Python TensorFlow.
  • To do this task we are going to use the tf.compat.v1.get_variable() function and the function will help the user to get the given or existing variable with this parameter. In this example, we will mention only the name and shape parameter.

Syntax:

Here is the Syntax of tf.compat.v1.get_variable() function

tf.compat.v1.get_variable
                         (
                          name,
                          shape=None,
                          dtype=None,
                          initializer=None,
                          regularizer=None,
                          trainable=None,
                          collections=None,
                          caching_devices=None,
                          partitioner=None,
                          validate_shape=true,
                          use_resource=None,
                          custom_getter=None,
                          constraint=None,
                          synchronization=tf.variableSynchronization.AUTO,
                          aggregation=tf.compat.v1.variableAggregation.None
                         )

Example:

Let’s take an example and check how to get the variable by name in Python TensorFlow.

Source Code:

import tensorflow as tf

with tf.compat.v1.variable_scope("tens1"):
    result = tf.compat.v1.get_variable("tens2", shape=[1])
    print(result)

In the above code we have imported the TensorFlow library and then use the tf.compat.v1.get_variable() function and within this function, we have assigned the variable name and shape parameter.

Here is the implementation of the following given code.

TensorFlow get variable by name
TensorFlow get variable by name

Read: TensorFlow get shape

TensorFlow get variable value

  • In this section, we will discuss how to get the value from variable in Python TensorFlow.
  • By using the tf.Variable() function, we can easily create the variable and assign the values to it. Next we will use the tf.compat.v1.global_variables_initializer() function and this method is used to initialize a global variables.
  • Next, we will create the session like tf.compat.v1.session() and launch the session with tf.session() as Val where Val is the session name.

Syntax:

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

tf.variable
           (
            initial_value=None,
            trainable=None,
            validate_shape=True,
            caching_device=None,
            name=None,
            variable_def=None,
            dtype=None,
            import_scope=None,
            constraint=None,
            synchronization=tf.VariableSynchronization.AUTO,
            aggregation=tf.compat.v1.variableAggregation.NONE
           )

Example:

Let’s take an example and check how to get the value from variables in Python TensorFlow.

Source Code:

import tensorflow as tf

tf.compat.v1.disable_eager_execution()
tens = tf.Variable([25, 29])
tens_init = tf.compat.v1.global_variables_initializer()
with tf.compat.v1.Session() as val:
    val.run(tens_init)
    result=val.run(tens)
    print(result)

Here is the execution of the following given code.

TensorFlow get variable value in Python
TensorFlow get variable value in Python

As you can see in the Screenshot the Output displays the values from the given variable.

Read: Python TensorFlow reduce_sum

TensorFlow get variable scope

  • In this Program, we will discuss how to get the variable scope in Python TensorFlow.
  • By using the tf.compat.v1.variable_scope() function, we can easily get the variable scope. In Python this function is used to define operation that declares variables(layers).

Syntax:

Let’s have a look at the Syntax and understand the working of the tf.compat.v1.variable_scope() function in Python TensorFlow.

tf.compat.v1.variable_scope
                           (
                            name_or_scope,
                            default_name=None,
                            values=None,
                            intializer=None,
                            regularizer=None,
                            caching_device=None,
                            partitioner=None,
                            custom_getter=None,
                            reuse=None,
                            dtype=None,
                            use_resource=None,
                            constraint=None,
                            auxiliary_name_scope=True
                           )

Example:

Let’s take an example and check how to get the variable scope in Python TensorFlow.

Source Code:

import tensorflow as tf

with tf.compat.v1.variable_scope('tens1'):
    result = tf.zeros((), name='tens2')
    print(result)

Here is the Output of the following given code.

TensorFlow get variable scope in Python
TensorFlow get the variable scope in Python

Read: Python TensorFlow reduce_mean

TensorFlow get variable initializer

  • In this section, we will discuss how to use the initializer parameter in tf.compat.v1.get_variable() function.
  • To do this task, we are going to use the tf.compat.v1.get_variable() function and within this function, we will set the initializer as an argument
  • This initializer specifies the tensor or object and if it is tensor then the given shape validate_shape will be false.

Syntax:

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

tf.compat.v1.get_variable
                         (
                          name,
                          shape=None,
                          dtype=None,
                          initializer=None,
                          regularizer=None,
                          trainable=None,
                          collections=None,
                          caching_devices=None,
                          partitioner=None,
                          validate_shape=true,
                          use_resource=None,
                          custom_getter=None,
                          constraint=None,
                          synchronization=tf.variableSynchronization.AUTO,
                          aggregation=tf.compat.v1.variableAggregation.None
                         )

Example:

Let’s take an example and check how to use the initializer parameter in tf.compat.v1.get_variable() function.

Source Code:

import tensorflow as tf
import numpy as np

tens=np.random.rand(4,5)
result=tf.compat.v1.get_variable('var_name',initializer=tens)
print(result)

Here is the implementation of the following given code

TensorFlow get variable initializer
TensorFlow get variable initializer

Read: Python TensorFlow random uniform

TensorFlow get variable list

  • Here we are going to discuss how to get the variable list in Python TensorFlow.
  • To do this task we are going to use the tf.compat.v1.get_collection() function and this function is used for the default graph.

Syntax:

Here is the Syntax of tf.compat.v1.get_collection() function.

tf.compat.v1.get_collection
                           (
                            key,
                            scope=None,
                           )
  • It consists of a few parameters
    • Key: This parameter indicates the key for the collection .
    • scope: It is an optional parameter the resulting list is filtered to include only elements.

Example:

Let’s take an example and check how to get the variable list in Python TensorFlow.

Source Code:

import tensorflow as tf

with tf.compat.v1.variable_scope('my_scope'):
    tens = tf.Variable(0)
    
print (tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.GLOBAL_VARIABLES, scope='my_scope'))

In the above code, we have imported the TensorFlow library and then use the tf.compat.v1.variable_scope() function (‘my_scope’).

Here is the Screenshot of the following given code.

TensorFlow get variable list
TensorFlow get a variable list

You may also like to read the following TensorFlow tutorials.

In this Python tutorial, we have learned how to get the variable in Python TensorFlow. Also, we have covered the following topics.

  • TensorFlow get variable by name
  • TensorFlow get variable value
  • TensorFlow get variable scope
  • TensorFlow get variable initializer
  • TensorFlow get variable list