Python ProgrammingPython Programming

TENSORFLOWVariables

This is the sixth tutorial in our TensorFlow tutorial series.

In TensorFlow when we develop a deep learning model, we need Variables to represent the parameters of the model. TensorFlow Variables are in-memory buffers that have tensors, Variables exist across multiple executions of a graph. A Variables value will vary as training goes ahead, and each variation will bring the model familiar to the desired system.

Characteristics of a Variable in TensorFlow:

  • A variable holds its value between subsequent executions of a session.
  • A variable must be initialized by an executing session.
  • A variable is an instance of the Variable class.
  • Variable can store data in multidimensional arrays and can be processed with TensorFlow operations.
  • Variables must be initialized explicitly before a graph is needed for the first time.
  • The values stored in variables can be save into the disk and we can restore them for further use.

Creating Variables

In TensorFlow, we can create variables by calling tf.Variable, the first parameter sets the variable's initial value and second optional parameter defines the name of variable.

# variable = tf.Variable(<initial-value>, name=<optional-name>)

For example, the following code creates 4 different variables, the variableA and sets its initial value equal to a tensor named tensorX, variableB and variableC have a static values. variableD have values of variableA and variableB.

##
# TensorFlow program with example of creating different variables.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
 
tensorX = tf.constant([1.5, 2.5, 3.5])
variableA = tf.Variable(tensorX, name="variableA")
 
variableB = tf.Variable([10, 20.5, 30.5], name="variableB")
variableC = tf.Variable(["US", "UK"], name="variableC")
variableD = tf.Variable([variableA, variableB], name="variableD")
 
print(format(variableA))
print(format(variableB))
print(format(variableC))
print(format(variableD))
 

Here is the output of above program.

C:\tf\examples\chapter6>pycodestyle --first example1.py
 
C:\tf\examples\chapter6>python example1.py
<tf.Variable 'variableA:0' shape=(3,) dtype=float32_ref>
<tf.Variable 'variableB:0' shape=(3,) dtype=float32_ref>
<tf.Variable 'variableC:0' shape=(2,) dtype=string_ref>
<tf.Variable 'variableD:0' shape=(2, 3) dtype=float32_ref>
 
C:\tf\examples\chapter6>

Variable Initialization

After creating a variables, we need to explicitly perform an initialization operation and execute it in a session, which allocates the memory for the Variable and sets its initial values. This induces the Session to start keeping track of the ongoing value of the Variable.

TensorFlow have global_variables_initializer() functions that create operations which initialize variables.

  • global_variables_initializer() - This function returns an operation which initializes all global variables.

The following code shows how variables has been initialized.

##
# TensorFlow program with example of create and initialize variables.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
 
tensorX = tf.constant([1.5, 2.5, 3.5])
variableA = tf.Variable(tensorX, name="variableA")
 
variableB = tf.Variable([10, 20.5, 30.5], name="variableB")
variableC = tf.Variable(["US", "UK"], name="variableC")
variableD = tf.Variable([variableA, variableB], name="variableD")
 
sess = tf.Session()
sess.run(tf.global_variables_initializer())
 
print(sess.run(variableA))
print(sess.run(variableB))
print(sess.run(variableC))
print(sess.run(variableD))
 

Here is the output of above program.

 
C:\tf\examples\chapter6>python example1.py
[1.5 2.5 3.5]
[10.  20.5 30.5]
[b'US' b'UK']
[[ 1.5  2.5  3.5]
 [10.  20.5 30.5]]
 
C:\tf\examples\chapter6>

Variable Assignment

In order to update, increment and decrement value of Variable, we use Variable.assign(), Variable.assign_add() and Variable.assign_sub() methods respectively.

##
# TensorFlow program with example of variable assignment.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
 
# Create a variable
variableA = tf.Variable(10, name="variableA")
 
# Start a session
sess = tf.Session()
 
# Initialization operation
init = tf.global_variables_initializer()
 
# Initialize variable
sess.run(init)
 
# Print session variable
print(sess.run(variableA))
 
# Assign new value to variable
sess.run(variableA.assign(20))
print(sess.run(variableA))
 
# Increment by 20
sess.run(variableA.assign_add(20))
print(sess.run(variableA))
 
# Decrement by 5
sess.run(variableA.assign_sub(5))
print(sess.run(variableA))
 

Here is the output of above program.

C:\tf\examples\chapter6>pycodestyle --first example2.py
 
C:\tf\examples\chapter6>python example2.py
10
20
40
35
 
C:\tf\examples\chapter6>