Thursday, December 26, 2024
Google search engine
HomeLanguagesDifference Between tf.Session() And tf.InteractiveSession() Functions in Python Tensorflow

Difference Between tf.Session() And tf.InteractiveSession() Functions in Python Tensorflow

In this article, we are going to see the differences between  tf.Session() and tf.InteractiveSession().

tf.Session()

In TensorFlow, the computations are done using graphs. But when a graph is created, the values and computations are not defined. So a session is used to run the graph. The sessions place the graphs on targeted devices and execute them. It finally returns tensors. In tf.Session() we have to define the whole skeleton of the graph before the session is started.  The entire calculations have to be mentioned in the graph before the session is initialized. We use sess.run(tensor_name) to execute each computation. 

Let us illustrate with the help of an example:

Python3




# importing tensorflow
import tensorflow as tf  
  
# A computational graph creation
# in which we take two tensors
# and perform summation of two tensors
  
# defining constant tensors
x = tf.constant(1)  
y = tf.constant(2)  
  
# summation of two tensors
z = x+y  
  
with tf.Session() as sess:
    print(sess.run(x))  
    print(sess.run(y)) 
    print(sess.run(z))


Output:

1
2
3

Explanation of the Code

In the above code, we create two tensors and define the addition operation. Then the session is defined and executed. Each computation is performed in the session. So first x and y are printed followed by a summation of x+y.

tf.InteractiveSession()

This session is much more interactive. It allows us to create dynamic graphs. In this, first the session is created and then the computational graph is created. The computations are executed using eval() function. An interactive session installs itself as the default session. Let us illustrate the InteractiveSession with the help of an example: 

Python3




# Declaring tensorflow library
import tensorflow as tf  
  
# interactive session
sess = tf.InteractiveSession()  
  
# creation of tensors
x = tf.constant(1)
y = tf.constant(2)
  
# addition operation of tensors
z = x+y
  
print(x.eval())
print(y.eval())
print(z.eval())
sess.close()


Output:

1
2
3

Explanation of the Code

In the above code, we first declare the interactive session and then the tensors.  However, we can define the tensors as well before calling the Interactive Session. Then we use eval function to evaluate each operation. Finally, we close the session.

Table of Difference between tf.Session() and tf.InteractiveSession()

tf.Session() tf.InteractiveSession()
 First we create the structure of the computational graph and then call the Session function. First, we call the Interactive session and then create the graph.
tf.Session() depends on another default session. tf.InteractiveSession() loads itself as the default session.
It is defined using tf.Session() It is defined using tf.InteractiveSession()

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments