Tuesday, November 19, 2024
Google search engine
HomeLanguagesVariables in Tensorflow

Variables in Tensorflow

TensorFlow is a Python library for efficient numerical computing. It’s a foundation library that can be used to develop machine learning and deep learning models. Tensorflow is a high-level library. A variable is a state or value that can be modified by performing operations on it. In TensorFlow variables are created using the Variable() constructor.

The Variable() constructor expects an initial value for the variable, which can be any kind or shape of Tensor. The type and form of the variable are defined by its initial value. The shape and the variables are fixed once they are created. let’s look at a few examples of how to create variables in TensorFlow.

Syntax: 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, shape=None)

Parameters:

  • initial_value: by default None. The initial value for the Variable is a Tensor, or a Python object convertible to a Tensor.
  • trainable: by default None.  If True, GradientTapes will keep an eye on this variable’s usage.
  • validate_shape: by default True. Allows the variable to be initialised with an unknown shape value if False. The shape of initial value must be known if True, which is the default.
  • name:by default None. The variable’s optional name. Defaults to ‘Variable’ and is automatically uniquified.
  • variable_def: by default None.
  •  dtype: by default None. If set, initial_value will be converted to the given type. If None, either the datatype will be kept (if initial_value is a Tensor), or convert_to_tensor will decide.
  •  shape: by default None. if None the shape of initial_value will be used. if any shape is specified, the variable will be assigned with that particular shape.

Creating a variable

tf.Variable() constructor is used to create a variable in TensorFlow.

Python3




tensor = tf.Variable([3,4])


Output:

<tf.Variable ‘Variable:0’ shape=(2,) dtype=int32, numpy=array([3, 4], dtype=int32)>

Dimension, size, shape, and dtype of a TensorFlow variable

Python3




# import packages
import tensorflow as tf
 
# create variable
tensor1 = tf.Variable([3, 4])
 
# The shape of the variable
print("The shape of the variable: ",
      tensor1.shape)
 
# The number of dimensions in the variable
print("The number of dimensions in the variable:",
      tf.rank(tensor1).numpy())
 
# The size of the variable
print("The size of the tensorflow variable:",
      tf.size(tensor1).numpy())
 
# checking the datatype of the variable
print("The datatype of the tensorflow variable is:",
      tensor1.dtype)


Output:

The shape of the variable:  (2,)
The number of dimensions in the variable: 1
The size of the tensorflow variable: 2
The datatype of the tensorflow variable is: <dtype: 'int32'>

Assigning or modifying the elements in the variable

We use the assign() method to modify the variable. It is more like indexing and then using the assign() method. There are more methods to assign or modify the variable such as Variable.assign_add() and Variable.assign_sub()).

Example 1:

assign(): It’s used to update or add a new value.

Syntax: assign(value, use_locking=False, name=None, read_value=True)

parameters:

  • value: The new value for this variable.
  • use_locking:  locking during assignment if “true”.

Python3




import tensorflow as tf
 
tensor1 = tf.Variable([3, 4])
tensor1[1].assign(5)
tensor1


Output:

<tf.Variable ‘Variable:0’ shape=(2,) dtype=int32, numpy=array([3, 5], dtype=int32)>

Example 2:

Syntax: assign_add(delta, use_locking=False, name=None, read_value=True)

parameters:

  • delta: The value to be added to the variable(Tensor).
  • use_locking: During the operation, if True, utilise locking.
  • name:  name of the operation.
  • read_value: If True, anything that evaluates to the modified value of the variable will be returned; if False, the assign op will be returned.

Python3




# import packages
import tensorflow as tf
 
# create variable
tensor1 = tf.Variable([3, 4])
 
# using assign_add() function
tensor1.assign_add([1, 1])
tensor1


Output:

<tf.Variable ‘Variable:0’ shape=(2,) dtype=int32, numpy=array([4, 5], dtype=int32)>

Example 3:

Syntax: assign_sub(  delta, use_locking=False, name=None, read_value=True)

parameters:

  • delta: The value to be subtracted from the variable
  • use_locking: During the operation, if True, utilise locking.
  • name: name of the operation.
  • read_value: If True, anything that evaluates to the modified value of the variable will be returned; if False, the assign op will be returned.

Python3




# import packages
import tensorflow as tf
 
# create variable
tensor1 = tf.Variable([3, 4])
 
# using assign_sub() function
tensor1.assign_sub([1, 1])
tensor1


Output:

<tf.Variable ‘Variable:0’ shape=(2,) dtype=int32, numpy=array([2, 3], dtype=int32)>

Changing the shape of the variable

tf.reshape() method is used to change the shape of the variable. the variable and shape must be passed. 

Python3




import tensorflow as tf
 
tensor1 = tf.Variable([[1, 2, 3, 4]])
tf.reshape(tensor1, shape=(2, 2))
tensor1


Output:

<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
       [3, 4]], dtype=int32)>

Changing the datatype of the tensor

If we want the variable to have a particular data type we have to specify dtype while creating the variable. in this example, we specified dtype to be float. 

Python3




import tensorflow as tf
 
tensor1 = tf.Variable([[1, 2, 3, 4]], dtype=float)
tensor1


Output:

<tf.Variable ‘Variable:0’ shape=(1, 4) dtype=float32, numpy=array([[1., 2., 3., 4.]], dtype=float32)>

Operations with Variables

We can perform addition, subtraction, multiplication, division, and many more operations with TensorFlow variables.

Python3




# import packages
import tensorflow as tf
 
# create two variables
tensor1 = tf.Variable([3, 4])
tensor2 = tf.Variable([5, 6])
print("Addition of tensors", tensor1+tensor2)
 
print("Subtraction of tensors", tensor1-tensor2)
 
print("Multiplication of tensors", tensor1*tensor2)
 
print("division of tensors", tensor1/tensor2)


Output:

Addition of tensors tf.Tensor([ 8 10], shape=(2,), dtype=int32)

Subtraction of tensors tf.Tensor([-2 -2], shape=(2,), dtype=int32)

Multiplication of tensors tf.Tensor([15 24], shape=(2,), dtype=int32)

division of tensors tf.Tensor([0.6        0.66666667], shape=(2,), dtype=float64)

Broadcasting

When we try to execute combined operations with several Variable objects, exactly like with Tensor objects, the variables which are smaller can extend out instantly to fit the variable large in size just like NumPy arrays can. When you try to multiply a scalar Variable with a  Variable,  the scalar is stretched to multiply each element of the Variable.

Python3




# import packages
import tensorflow as tf
 
# create two variables
tensor1 = tf.Variable([3, 4])
tensor2 = tf.Variable([2])
 
# broadcasting
output = tensor1*tensor2
print(output)


Output:

tf.Tensor([6 8], shape=(2,), dtype=int32)

Hardware Selection for Variables

We may utilize it to see what type of device (i.e., processor) is used to process our variable. .device attribute is used.

Python3




# import packages
import tensorflow as tf
 
# create a variable
tensor1 = tf.Variable([3, 4])
 
print('The type of hardware variable used : '+tensor1.device)


Output:

The type of hardware variable used : /job:localhost/replica:0/task:0/device:CPU:0

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments