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)

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.

Similar Reads

Creating a variable

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

Assigning or modifying the elements in the variable

...

Changing the shape of the variable

...

Changing the datatype of the tensor

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())....

Operations with Variables

...

Broadcasting

...

Hardware Selection for Variables

...

Contact Us