## Introduction to Tensorflow variable

The following article provides an outline for the Tensorflow variable. All operations in TensorFlow use tensors, and all values in a tensor have the same data type. As a result, TensorFlow predicts the shape when you print tensor. On the other hand, the TensorFlow shape property can be used to obtain the tensor’s shape.

### What is the TensorFlow variable?

When building a deep learning model in TensorFlow, we require Variables to represent the model’s parameters. TensorFlow Variables are tensor-based in-memory buffers that persist across several graph executions. As training progresses, the value of a variable will change, and each variation will get the model closer to the ideal system.

All computations in TensorFlow go through one or more sensors. A tensor can be expressed in a scalar format or with a multidimensional format. A tf.tensor is a three-property object that includes:

- A distinctive label (name)
- a measurement (shape)
- a type of data (dtype)

Variable characteristics

- Between executions of a session, a variable retains its value.
- An executing session must initialise a variable.
- Variables are objects that belong to the Variable class.
- Variables can be used to save data in arrays and to proceed it using TensorFlow operations.
- Before a graph may be used for the first time, variables must be explicitly initialized.
- We can save the values saved in variables to disc and then retrieve them for later use.

### Create a variable

The Variable class can be used to reflect the fact that data always arrives with different values. It will depict a node with constantly changing values. We can use the tf.getvariable() method to generate a variable. Variable initializers must be run before any other operations in your model can be executed. The simplest approach to achieve this is to create an op that executes all of the variable initializers before using the model.

`Var1 = tf. Variable (<starting Val>, name=<optional>)`

The first parameter determines the variable’s initial value, while the second optional parameter defines the variable’s name.

library (tensor flow)

`my_var <- tf$Variable (tf$zeros (shape (1,2,3)))`

The above format creates three-dimensional shapes done with zeros. They have a default dtype tf@float32. Most TensorFlow optimizers feature specialised ops that update variable values efficiently using a gradient descent-like technique.

`<- tf$Variable (0)`

var$assign_add(10)

Variables in TensorFlow must be initialised before they may be used. Constant tensors are the polar opposite of constant tensors.

11 Online Courses | 3 Hands-on Projects| 55+ Hours| Verifiable Certificate of Completion

4.5

View Course

`We = tf. Variable(tf. zeros((2,2)), name="weights")`

Res = tf.Variable(tf.random_normal((2,2)), name="random weights")

with tf.Session() as ses1:

ses1.run(tf.initialize_all_variables())

print(ses1.run(We))

print(ses1.run(Res))

Here weights is a variable object that could be assigned from a constant. Hundreds of variables can be found in complicated TensorFlow models.

To avoid collisions, tf.variable scope() provides simple name-spacing.

Within a variable scope, tf.get variable() creates/accesses variables.

### TensorFlow variable Lifecycles

The lifecycle of a tf.Variable instance in Python-based TensorFlow is the same as that of other Object storage. When a variable has no references, it is immediately deallocated. Variables can also be given names, which makes them easier to track and debug. Two variables with the same name are allowed.

`.m= tf.Variable(my_tensor, name="John")`

n= tf.Variable(my_tensor + 1, name="John")

print (m == n)

Therefore, this produces

`tf.Tensor(`

[[False False]
[False False]], shape= (2, 2), dtype=bool)

When we declare a Variable, we may use the tf.assign() function to alter its value in the future, and we can use a value or an action to initialise it.

The function tf.global variables initializer () initializes all variables in the code with the value supplied as a parameter, but it does it in async mode, which means it doesn’t operate properly when variables have dependencies.

Next, we shall see how to update a variable.

`Updating Variable State`

state = tf.Variable(0, name="increase")

nval = tf.add (state, tf. constant (1))

update = tf. assign (state, nval)

with tf.Session() as se1:

se1.run(tf.initialize_all_variables ())

print(se1.run(state))

for i _ in range(3):

se1.run(update)

print(se1.run(state))

### TensorFlow variable Examples

Different examples are mentioned below:

#### Example #1 – Using the loss of training

`x_hat = tf.constant(36, name='x_hat')`

x = tf.constant(39, name='x')

loss = tf.Variable((x - x_hat)**2, name='loss')

init = tf.global_variables_initializer()

tf.Session() as session:

session.run(init)

print(session.run(loss))

**Explanation**

loss=(-y). Defining a constant to 36 and creating a variable for loss manipulation. Next, creating a session and output is displayed.

**Output**

**9**

#### Example #2

`from __future__ import absolute_import`

from __future__ import division

from __future__ import print_function

import tensorflow as tf

va1 = tf.Variable(11, name="va1")

se1 = tf.Session()

init = tf. global_variables_initializer ()

se1.run(init)

print(se1.run(va1))

se1.run(va1.assign(12))

print(se1.run(va1))

se1.run(va1.assign_add(12))

print(se1.run(va1))

se1.run(va1.assign_sub(4))

print(se1.run(va1))

**Explanation**

The tensor uses a variable to initialize a value in the above code. Next, add, and sub-functions are used to increment and decrement an assigned tensor value.

**Output**

#### Example #3

`import tensorflow as tf`

vble = tf.get_variable("vble", [1, 2])

print(vble)

var_i = tf.get_variable("var_i", [1, 2], dtype=tf.int32, initializer=tf.zeros_initializer)

print(var_i)

t_ct = tf.constant([[11, 12],[13, 14]])

var_2 = tf.get_variable("var_2", dtype=tf.int32, initializer=t_ct)

print(var_2)

**Explanation**

Because the shape of ‘initializer’ is used, there is no need to include the ‘values’ if the initializer is supplied. The above code’s output is as follows:

**Output**

#### Example #4

`from __future__ import absolute_import`

from __future__ import division

from __future__ import print_function

import tensorflow as tf

tens1 = tf.constant([2.1, 3.1, 4.1])

varX = tf.Variable(tens1 , name="varX")

varY = tf.Variable([12, 21.1, 34.5], name="varY")

varZ = tf.Variable(["AU", "IND"], name="varZ")

varZY = tf.Variable([varX, varY], name="varZY")

print(format(varX))

print(format(varY))

print(format(varZ))

print(format(varZY))

**Explanation**

Here a tensor creates four different variables with values. The output takes the shape of the variable to display an output.

**Output**

#### Example #5 – Variable Assignment

`from __future__ import absolute_import`

from __future__ import division

from __future__ import print_function

import tensorflow as tf

varM = tf.Variable(5, name="varM")

se1 = tf.Session()

init = tf.global_variables_initializer ()

se1.run(init)

print(se1.run(varM))

se1.run(varM.assign(15))

print(se1.run(varM))

se1.run(varM.assign_add(10))

print(se1.run(varM))

se1.run(varM.assign_sub(4))

print(se1.run(varM))

** **

**Explanation**

Generally, we utilize the Variable.assign(), Variable.assign add (), and Variable.assign sub () methods to update, increment, and reduce the value of Variable. In the above code, we have used Tensor flow with a variable assignment where we first create a variable varM and started a session by initialization. Next, to assign a new value to a session, use the assign () method. And the output is shown below after increment and decrement operation.

**Output**

#### Example #6

`import numpy as np`

import tensorflow as tf

zz= tf.Variable(0, dtype=tf.float32)

cost = 15 + 3*zz + zz*zz

tr1 = tf.train.GradientDescentOptimizer(0.01). minimize(cost)

init = tf.global_variables_initializer()

se1 = tf.Session()

se1.run(init)

print(se1.run(zz))

se1.run(tr1)

print(se1.run(zz))

for j in range (10000):

se1.run(tr1)

print(se1.run(zz))

**Explanation**

The Variable was initially set to 0, then changed to -0.049999997 after only one step of gradient descent, and finally to -2.499994 after 10.000 steps. An interactive session is essential when numerous separate sessions are executed in the same script.

**Output**

### Conclusion

Finally, this article discusses the tensor flow variables and their initialization in depth. We’ve also seen a few examples of how variables affect a tensor flow in Python. To initialize a variable as a constant, we must call a function Object (). the initial value can be handed in as an argument. Object() variables may be simply added to the computational graph by using a function.

### Recommended Articles

This is a guide to Tensorflow variable. Here we discuss a few examples of how variables affect a tensor flow in Python and the tensor flow variables and their initialization in depth. You may also have a look at the following articles to learn more –