Understanding tf.Variable() in TensorFlow Python
Hey Everyone,
We are going to take a look at the tf.Variable()
function in TensorFlow and will understand this function with the help of this tutorial. Let’s first take a look at this TensorFlow library in Python.
What is TensorFlow?
TensorFlow is a library in Python. It is used not only in machine learning but also in deep learning. You can install this library using the command mentioned below:
pip install tensorflow
TensorFlow library is effective for efficient numerical computing in Python. A variable is nothing but a value that is changeable during operations. In the TensorFlow library, we use the Variable() constructor we create these variables.
Now, Let’s just understand more about this Variable()
constructor.
tf.Variable() in TensorFlow
The Variable() constructor takes an initial value for the variable of any kind i.e. any shape of the tensor. There is an initial value for variables. It defines the type and form of the variable. The shape of the variable is fixed as they are created. Now, let’s just see what is the syntax for the Variable() constructor.
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)
Now let’s just look at these parameters in the Variable() constructor one by one.
Parameters:
- initial_value: It is a tensor. It is by default None.
- trainable: Manages variables usage, if True. It is by default None.
- validate_shape: Variable can be initialized to an unknown shape value if False. It is by default True.
- name: name for the variable(optional). It is by default None
- variable_def: None.
- dtype: 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. By default it is None.
- shape: if any shape is specified, the variable will be assigned to that particular shape. It is by default None means the initial_value’s shape will be used.
Creating a Variable
Now, let’s see the code for tf.Variable() constructor
import tensorflow as tf input_tensor = tf.Variable([9, 6]) print("Shape: ",input_tensor.shape) print("Number of dimensions:",input_tensor.rank(tensor1).numpy()) print("Size :",tf.size(input_tensor).numpy()) print("Datatype:",input_tensor.dtype)
Output:
Shape : (2,) Number of dimensions : 1 Size : 2 Datatype: <dtype: ‘int32’>
Assigning the elements in variable
We will assign elements to the variable using the assign() function. The function assign()
is used to update or add new values.
Now let’s write the code for this.
import tensorflow as tf input_tensor = tf.Variable([9, 6]) input_tensor [1].assign(8) input_tensor
Output:
<tf.Variable 'Variable:0' shape=(2,) dtype=int32, numpy=array([9, 8])>
We can also use the assign_add()
method on the variable. This method adds value to the already created variable
input_tensor.assign_add([1, 1]) input_tensor
Output:
<tf.Variable 'Variable:0' shape=(2,) dtype=int32, numpy=array([10, 9])>
We can also perform operations like addition or multiplication on the variables. Now let’s just see the code for operations on the variable.
Operations with Variables
We can perform arithmetic as well as other operations with TensorFlow variables.
input_tensor1 = tf.Variable([9, 6]) input_tensor2 = tf.Variable([1, 5]) print("Addition:", input_tensor1+input_tensor2) print("Subtraction:", input_tensor1-input_tensor2) print("Multiplication:", input_tensor1*input_tensor2) print("Division:", input_tensor1/input_tensor2)
Output:
Addition: tf.Tensor([10 11], shape=(2,), dtype=int32) Subtraction: tf.Tensor([8 1], shape=(2,), dtype=int32) Multiplication: tf.Tensor([ 9 30], shape=(2,), dtype=int32) Division: tf.Tensor([9. 1.2], shape=(2,), dtype=float64)
Leave a Reply