Julien
Julien

Reputation: 781

TensorFlow - Diffrence between Session() and Session(Graph())

To run a programm with TensorFlow, we must declare a session.

So what is the difference between sess = Session() and sess = Session(Graph()) ?

What is this Graph() ?

Upvotes: 1

Views: 393

Answers (2)

prosti
prosti

Reputation: 46341

Designing a model in TensorFlow assumes these two parts:

Building graph(s), representing the data flow of the computations.

Running a session(s), executing the operations in the graph.

In general case, there can be multiple graphs and multiple sessions. But there is always one default graph and one default session.

In that context sess = Session() would assume the default session:

If no graph argument is specified when constructing the session, the default graph will be launched in the session.

sess = Session(Graph()) would assume you are using more than one graph.

If you are using more than one graph (created with tf.Graph() in the same process, you will have to use different sessions for each graph, but each graph can be used in multiple sessions. In this case, it is often clearer to pass the graph to be launched explicitly to the session constructor.

Upvotes: 0

vaquar khan
vaquar khan

Reputation: 11449

When designing a Model in Tensorflow, there are basically 2 steps

  1. building the computational graph, the nodes and operations and how they are connected to each other
  2. evaluating / running this graph on some data

A Session object encapsulates the environment in which Operation objects are executed, and Tensor objects are evaluated. For example:

# Launch the graph in a session.
sess = tf.Session()

# Evaluate the tensor `c`.
print(sess.run(c))

Upvotes: 3

Related Questions