Variables in TensorFlow

In this tutorial I will teach you to create and use Variables in TensorFlow.

Variables in TensorFlow

Variables in TensorFlow - Defining and using variables in TensorFlow

In this section we will learn how to create and use variables in TensorFlow. TensorFlow programming language is supports python programming and we are using python for writing TensorFlow program.

In python we can create variables in different ways and use in the python program. But the problem here is that we can't use the python variables in TensorFlow, we have to convert the python variables to TensorFlow variables and then use.

For example if you have a numpy variable:

data = np.random.randint(100, size=100)

Then you can use the following function to convert into Tensor (Tensor is the TensorFlow variable). In TensorFlow Tensor is the single or multidimensional data.

In the following program we are converting numpy data to a TensorFlow Tensor:

import tensorflow as tf
import numpy as np
data = np.random.randint(100, size=100)
print(data)

dataTF = tf.convert_to_tensor(data)

print(dataTF)

If you run the program it will give following output:

C:\Users\Dell>python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> import numpy as np
>>> data = np.random.randint(100, size=100)
>>> print(data)
[76 32  1 37 49 13 48 22 81 28 77 43 67 49 26 91 36 68 89 39 81 30 54  1 33
 54 88 36 15 43  7 66 71 27 58 80 78 20 71 19 80 39  3 94 64 73 63  5 25 95
  5 88 89 30 23 73 15 54 95 19 21 27 38 13 11 80 11  1 97 10 79 80 91 19 52
  4 52 76 38 15 23  1 90 78 51 29 56 17 33 43 90 30 20 95 19 40  6 80  6 19]
>>>
>>> dataTF = tf.convert_to_tensor(data)
>>>
>>> print(dataTF)
Tensor("Const:0", shape=(100,), dtype=int32)
>>>
>>>

Variable types in TensorFlow

In TensorFlow we have following types of variable types:

  • Variable: These are used to store data and it can be modified. The method tf.Variable() is used to create variable.
  • Constant: These are immutable variable and can't be modified. The method tf.Constant() is used to create constant.
  • Placeholder: These are special type of variable in TensorFlow which is used in runtime to pass values to Tensors. The tf.placeholder() method is used to create placeholder.

Example of variable in TensorFlow

In the following example we are creating two variables and then multiplying it with the tf.multiply() function. Here is the full source code:

import tensorflow as tf

a = tf.Variable(4,)
v = tf.Variable(2,)
b = tf.multiply(a, v)

sess = tf.Session()
model = tf.global_variables_initializer()

sess.run(model)

print(sess.run(b))

If you run the above code it will display following result:


C:\Users\Dell>
C:\Users\Dell>python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>>
>>> a = tf.Variable(4,)
>>> v = tf.Variable(2,)
>>> b = tf.multiply(a, v)
>>>
>>> sess = tf.Session()
>>> model = tf.global_variables_initializer()
>>>
>>> sess.run(model)
>>>
>>> print(sess.run(b))
8
>>>
>>>

Example of Constant

In TensorFlow constants are created with the function tf.constant(). Here is an example of defining constant and then multiplying it with the multiply function:

#Constant example
import tensorflow as tf

a = tf.constant(4,)
v = tf.constant(2,)
b = tf.multiply(a, v)

sess = tf.Session()
model = tf.global_variables_initializer()

sess.run(model)

print(sess.run(b))

If you run the program it will give following output:

C:\Users\Dell>python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>>
>>> a = tf.constant(4,)
>>> v = tf.constant(2,)
>>> b = tf.multiply(a, v)
>>>
>>> sess = tf.Session()
>>> model = tf.global_variables_initializer()
>>>
>>> sess.run(model)
>>>
>>> print(sess.run(b))
8
>>>
>>>
>>>
>>>

Example of placeholder

Now we will show you the placeholder example in TensorFlow. The placeholder is used to pass the values in runtime to the Tensors. You can use the method tf.placeholder() to define a place holder. Here is the full example code to multiply two tensors:

#Placeholder example
import tensorflow as tf

a = tf.placeholder(tf.float32, shape=[1])
v = tf.placeholder(tf.float32, shape=[1])
b = tf.multiply(a, v)

sess = tf.Session()
model = tf.global_variables_initializer()

sess.run(model)

print(sess.run(b,feed_dict={a: (4,), v:(5,)}))

If you run the above example it will print following output:

C:\Users\Dell>python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> #Placeholder example
... import tensorflow as tf
>>>
>>> a = tf.placeholder(tf.float32, shape=[1])
>>> v = tf.placeholder(tf.float32, shape=[1])
>>> b = tf.multiply(a, v)
>>>
>>> sess = tf.Session()
>>> model = tf.global_variables_initializer()
>>>
>>> sess.run(model)
>>>
>>> print(sess.run(b,feed_dict={a: (4,), v:(5,)}))
[ 20.]
>>>

In the above example we have created two placeholders with the following code:

a = tf.placeholder(tf.float32, shape=[1])
v =
tf.placeholder(tf.float32, shape=[1])

Then passed the values while calling the run method in runtime using the feed_dict variable as shown below:

print(sess.run(b,feed_dict={a: (4,), v:(5,)}))

In this tutorial you leaned to use constants, variables and placeholders in TensorFlow.