**Tensorflow variables are essential because these are used to store the parameters of machine learning models, for example, weights and biases in neural networks.**

Also, when models are trained using different algorithms, the variables’ values are updated many times, and these variables act as parameters for machine learning models.

So, here, I will explain tensorflow variables and how to create them using the **tf.Variable()** function with different properties that this variable has.

After, I will show you how to update or change the value of existing variables. You will also learn how to perform mathematical operations on these variables, such as addition, multiplication, subtraction, etc.

Specifically, I am going to explain the following topics:

- What is the TensorFlow variable?
- Creating TensorFlow Variable
- Reassigning value to TensorFlow Variable
- Creating Different Types of TensorFlow Variables
- Operation on TensorFlow Variable

## What is the TensorFlow variable?

**The general meaning of a variable is the value of a variable can vary (change) through the program’s execution. The TensorFlow variable works on the same concept; variables store the value, which can be read and modified anytime through an operation.**

In TensorFlow, while training machine learning models, it reads and updates data; these data or values are stored in a variable, so the Variable is a core concept in building and training machine learning models.

## Creating TensorFlow Variable

To create a variable in a TensorFlow, **tf.Varaible()** constructor is used, same as the **tf.constant(),** You can initialize it or pass a value with different properties such as **shape**, **dtype**, and **name**.

**Remember, the Variable () constructor is in tensorflow, so here it is written like this tf.Variable() means accessing the Variable() constructor from tensorflow (tf).**- The
**tf.Variable**is a kind of mutable tensor, and the value of this tensor can be changed during computing.

To create a variable in tensorflow, use the syntax below.

`tf.Variable(value, shape=(), dtype=datatype, name='variable')`

Where the properties are,

**value:**It is the initial value the variable stores.**shape:**It is the shape of the Variable in a tuple ().**dtype:**Datatype of elements in variable.**name:**It is the name of the Variable.

If you are unclear about the **shape**, **dtype**, etc., in TensorFlow, visit the tutorial Tensor in Tensorflow.

Now, let’s move and see how to create a variable with the initial value. For example, if you have value **[6, 1, 9, 3]**, to make a variable with this value, pass this value to **tf.Variable()** function as shown below.

`integer_variable = tf.Variable([6, 1, 9, 3])`

Print the value of a variable using the code below.

```
print(integer_variable)
```

Using the above code, first create a variable named **integer_variable** using the **tf.Variable()** function and initializing the Variable with a list of values **[6, 1, 9, 3]**, and then printing the variable properties value using the **print(integer_variable) **function.

In the output, we get four values.

- First is the
**‘Variable:0’**, which is the name of the Variable; - The second is the shape of the Variable, which is 4, and represented using tuple
**‘(4, )’**; - The third is the dtype, which is the data type of variable elements, which int32 (integer),
- Last is the elements of the Variable, which is an array of elements
**[6, 1, 9, 3]**.

The above properties can also be provided while initializing the variable value. For example, you have a list of values **[2, 4, 7]**.

Pass the above list of values to **tf.Variable()** with properties such as **shape as (3,)**, **dtype as float32** (for decimal value), and **name as ‘Float_Variable’**.

```
float_var = tf.Variable([2.0, 4.0, 7.0], shape=(3,), dtype=tf.float32, name = 'Float_Variable')
print(float_var)
```

As you can see, variable **float_var** is created with the name **‘Float_Variable’, shape(3, ),** and dtype of float32 (here accessing float32 dtype from tf), and the value of the Variable is **[2.0, 4.0, 7.0].**

Remember, if you only pass the value to **tf.Variable()** function automatically infers all properties such as **shape**, **dtype**, and **variable name** based on the value.

You learned how to create a tensorflow variable using the **tf.Variable() **constructor from the above example.

### Reassigning value to TensorFlow Variable

As you know, a variable’s value can change using an operation. I will show how to assign a new value to an existing variable here.

First, create a variable with an initial value **[2, 5, 7, 8]** as shown below.

```
var = tf.Variable([2, 5, 7, 8])
print(var)
```

But, here, I want to show you how to get only the Variable’s value; as you can see in the output, it displays information such as **shape**, **dtype**, etc., which is unnecessary this time.

To get only the value from the Variable, you need to call a method numpy() on the Variable, as shown below.

`print(var.numpy())`

Look at the output; when the **numpy()** method is called on the Variable named **var**, it returns only the Variable’s value, which is **[2, 5, 7, 8]**.

Let’s change the value of variable **var**. Suppose you have a list of values as **[3, 3, 6, 9]**, and you need to assign this value to the existing variable **var**.

To change the value of a variable, call the method assign() on the variable **var** and pass the new value to it as **assign([3, 3, 6, 9])**, as shown below.

`var.assign([3, 3, 6, 9])`

View the new value of variable **var** using the code below.

`print('Update Value',var.numpy())`

The value of variable var is changed to **[3, 3, 6, 9]** in the above output. So here, you have modified the value of an existing variable using the **assign() **method on the Variable.

Till now, you have created only two types of variables, which contain only a list of integer and float values, but you can make any type of Variable, such as boolean, complex, strings, etc.

### Creating Different Types of Tensorflow Variables

TensorFlow allows you to create different types of variables using the **tf.Variable() **function. Look at the below example.

Create a variable with an integer value, as shown below.

```
integer_var = tf.Variable(42)
print(integer_var)
```

Create a variable with a float value, as shown below.

```
float_var = tf.Variable(200.10)
print(float_var)
```

Create a variable with another tensor, as shown below.

```
tensor = tf.constant([5, 1, 3])
variable_tensor = tf.Variable(tensor)
print(variable_tensor)
```

Create a variable with a string value, as shown below.

```
str_variable = tf.Variable("Learning Tensorflow Variable")
print(str_variable)
```

Create a variable with a boolean value, as shown below.

```
bool_variable = tf.Variable(True)
print(bool_variable)
```

From the above example, you have seen how to create different types of variables, and also you can create sparse or complex variables.

Next, I want to show you how to operate on the tensorflow variables such as addition, subtraction, etc.

### Operation on TensorFlow Variable

The operations on TensorFlow mean mathematical operations like **addition**, **multiplication**, **subtractions**, etc. Also, the neural network operations include convolution and activation functions.

But here, I am not going to explain neural network operation. Instead, I will explain fundamental operations you can perform on TensorFlow variables.

Let’s start,

#### Addition Operation on Tensorflow Variable

You can add more than one tensorflow Variable into a single variable; for that, look at the example below.

First, define two tensorflow variables, **var1** and **var2**, and initialize them with **20.0** and **40.0**, respectively, as shown below.

```
var1 = tf.Variable(20.0)
var2 = tf.Variable(40.0)
```

To add the variables, create a third variable and use the **plus (+) operator** to combine the **var1** and **var2**, as shown below.

`var3 = var1 + var2`

Now check and print the value var3 using the code below.

`print(var3.numpy())`

The **value of var3 is 60.0**, the sum of the value in variables var1 and var2. You have added two scalar values into a single value using the plus (+) operator.

You can also add a scalar value (single number) to a variable. Suppose you have a list of values in a variable such as **[2,4,6]**, and you want to **add 1 to each element**. After adding 1, the list should be like this **[3, 5, 7]**. For that, run the code below.

```
vector_variable = tf.Variable([2,4,6])
added_value = vector_variable + 1;
print(added_value.numpy())
```

After **adding 1 to vector_variable** containing a list of values, each element is incremented by **1** **[ 3, 5, 7].**

You can even add two variables containing a list of values; look at the code below.

```
vector_var1 = tf.Variable([2,4,6])
vector_var2 = tf.Variable([2,4,6])
vector_var3 = vector_var1 + vector_var2
print(vector_var3.numpy())
```

As in the output, you can add the corresponding element of each list and, as a result, **[4, 8, 12]**.

- One thing you should note here is always try to add the variables containing the same number of elements.

If one Variable contains 3 elements and the other contains 2 or 4 elements, then both can’t be added. It will show an error like **‘Incompatible shapes’**.

For example, look at the variables in the below code.

```
vector_var1 = tf.Variable([2,4,6])
vector_var2 = tf.Variable([2,4,6,4])
vector_var3 = vector_var1 + vector_var2
print(vector_var3.numpy())
```

As you can see, it shows an error like **InvalidArgumentError**, which means **Incompatible shapes: [3] vs [4]**, which represents one Variable **(vector_var1) **contains 3 elements, another variable **(vector_var3)** 4 elements, so the addition is not possible.

You can even perform addition variables containing a list of lists or on higher dimensions.

## Subtraction Operation on Tensorflow Variable

You can also perform subtraction on tensorflow variables; for example, define two variables and subtract one from the other using the minu (-) operator, as shown in the code below.

From the output, **var2 **is subtracted from **var1** using the **minus (-) operator**, and as a result, it returns **20.0**.

Use the code below to subtract the scalar value 1 from the list of values [3,4,9] in the TensorFlow variable.

```
vector_variable = tf.Variable([3,4,9])
subtracted_value = vector_variable - 1;
print(subtracted_value.numpy())
```

1 is subtracted from each element in **vector_variable**; as a result, it contains values [2, 3, 8].

Next, you can subtract two tensorflows containing a list of values, as shown below.

```
vector_var1 = tf.Variable([2,4,6])
vector_var2 = tf.Variable([1,3,2,])
vector_var3 = vector_var1 - vector_var2
print(vector_var3.numpy())
```

The values in **vector_var2 **are subtracted from **vector_var1. **As a result, it gives **[1, 1, 4]**. Always pass the Variable of having the same shape. The subtraction operation will fail if you pass the two variables containing an unequal number of elements.

## Multiplication Operation on Tensorflow Variable

As you have performed addition and subtraction on tensorflow variables, the same way you can perform multiplication operations.

So, here, I am not going to explain each example. Instead, I will create two variables containing values and perform multiplication.

To multiply two scalar variables, use the asterisk (*) operator; look at the code below.

```
import tensorflow as tf
var1 = tf.Variable(10.0)
var2 = tf.Variable(10.0)
var3 = var1 * var2
print(var3.numpy())
```

Multiplication of **var1** and **var2 **returns the results as 100. Next, let’s multiply a number, a list of values in a variable, as shown below.

```
vector_variable = tf.Variable([1,3,6])
multiplied_value = vector_variable * 2;
print(multiplied_value.numpy())
```

Here, number 2 is multiplied with variable **vector_variable**; as a result, 2 is multiplied with each element in a variable and returns the result [2, 6, 12].

You can also multiply two variables containing a list of values, as shown below.

```
vector_var1 = tf.Variable([3,5,2])
vector_var2 = tf.Variable([2,3,2])
vector_var3 = vector_var1 * vector_var2
print(vector_var3.numpy())
```

You can see two variables containing the same number of elements are multiplied, as a result, giving a new list of values [6, 15, 4].

## Division Operation on Tensorflow Variable

Lastly, you can perform division operations on tensorflow variables using ‘/’. For example, look at the examples below.

The division of two variables contains a scalar value, as shown below.

```
import tensorflow as tf
var1 = tf.Variable(5)
var2 = tf.Variable(2)
var3 = var1 / var2
print(var3.numpy())
```

The **division of variables var1 and var2 gives result 2.5**, as shown in the output. The two variables are divided using the **‘/’**.

Next, let’s see how to divide the list of values in a TensorFlow variable with a number; run the code below for that.

```
vector_variable = tf.Variable([5,3,2])
divided_value = vector_variable / 2;
print(divided_value.numpy())
```

As you can see, each element of **vector_variable** **is divided by 2 single numbers **and gives a new value, which is **[2.5, 1.5, 1.]**.

Now, let’s divide the two variables containing a list of values, as shown below.

```
vector_var1 = tf.Variable([10,20,40])
vector_var2 = tf.Variable([2,4,5])
vector_var3 = vector_var1 / vector_var2
print(vector_var3.numpy())
```

The two variables **vector_var1** and **vector_var2** are divided using the **‘/’**; after division, the new value is returned, which is **[5., 5., 8.]**.

This is how to perform fundamental mathematical operations on variables in tensorflow.

## Conclusion

Knowing how to create TensorFlow variables and change their value or perform operations on them, such as addition, subtraction, multiplication, and division, is necessary for Tensorflow developers.

The variables are essential as the data flows through the tensorflow program to another program.

You may like to read:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etcâ€¦ for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.