Pages

Saturday, 8 June 2019

Constants, Placeholders, Variables and Sessions in TensorFlow

In this article on TensorFlow, we will see how to build and run a graph taking simple examples of constants, placeholders and variables. We will also learn something about sessions and feed dictionary. In order to learn some theory about TensorFlow, you can look at my this post. You can download my Jupyter notebook containing following code from here.

First thing first, lets import the TensorFlow library.

import tensorflow as tf

Constants

We have declared three nodes (a, b and c). Operations being performed at node "a" and "b" are to declare constant values. Node "c" is performing multiplication operation. 

a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b

Till now, we have just built a graph. We need to run it. In order to run a graph in TensorFlow, we need to create a session. Following is a way to create a session and run the graph.

session = tf.Session()
result = session.run(c)
print(result)
session.close()

Output: 30.0

Another example

We can also pass the data type to a node.

a = tf.constant(5.0, tf.float32)
b = tf.constant(6.0)
print(a,b)

If we print the nodes before running the graph, we will get following output:

Output: Tensor("Const_2:0", shape=(), dtype=float32) Tensor("Const_3:0", shape=(), dtype=float32)

To print the actual values, we need to create a session and run the graph.

session = tf.Session()
result = session.run([a,b])
print(result)
session.close()

Output: [5.0, 6.0]

Placeholders

We provide values to placeholders while running the graph.

a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
c = a * b
d = 2 * a

session = tf.Session()
result = session.run(c, {a:[1,3], b:[2,4]})
print(result)

Output: [ 2. 12.]

Feed Dictionary: We can also create a dictionary and input it into a placeholder while running a graph. We use feed_dict parameter for this.

dictionary = {a:[[[1,2,3],[4,5,6],[7,8,9]],[[1,2,3],[4,5,6],[7,8,9]]]}
result = session.run(d, feed_dict=dictionary)
print(result)
session.close()

Output:

[[[ 2.  4.  6.]
  [ 8. 10. 12.]
  [14. 16. 18.]]

 [[ 2.  4.  6.]
  [ 8. 10. 12.]
  [14. 16. 18.]]]

Naming a node

We can also provide a name to a node. This is helpful in visualizing the nodes in Tensor Board.

a = tf.placeholder(tf.float32, name="A")
b = tf.placeholder(tf.float32, name="B")
c = tf.multiply(a, b, name="C")

with tf.Session() as session:
    result = session.run(c, feed_dict={a:[1,2,3], b:[4,5,6]})
    print(result)

Output: [ 4. 10. 18.]

Variables

Lets create some nodes and assign them some operations.

zero = tf.Variable(0)
one = tf.constant(1)
new_value = tf.add(zero, one)
updated_variable = tf.assign(zero, new_value)

We must initialize all the variables while running the graph. So, following line is must.
init = tf.global_variables_initializer()
Now, create a session and run the graph.
session = tf.Session()
session.run(init) 
print(session.run(zero)) 
print(session.run(one)) 
print(session.run(new_value)) 
print(session.run(updated_variable))

Output:

0
1
1
1

Lets run the "updated_variable" node 5 times and observe the results.

for _ in range(5):
    session.run(updated_variable)
    print(session.run(zero))

Output:

2
3
4
5
6

Finally, close the session.

session.close()  

Strings

Below is the illustration of the string concatenation operation in TensorFlow.

hello = tf.constant('hello')
world = tf.constant('world')
hello_world = tf.add(hello, world)
with tf.Session() as session:
    print(session.run(hello_world))

Output: b'helloworld'

No comments:

Post a Comment

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.