Should tf.Session() fall inside the context of tf.Graph()?

Viewed 1360

I often see the following patterns in Tensorflow code, but frequently, I get better performance by ignoring them.

with tf.Graph().as_default():

    # Build graph here ...
    loss, train_op = ...

    with tf.Session() as sess: # OR: with sv.managed_sess() as sess, etc.

        # Run training steps here ...
        sess.run(train_op)

But I prefer to first define my graph and then separately run the session as follows (especially in Jupyter notebooks).

In one cell:

with tf.Graph().as_default():

    # Build graph here ...
    loss, train_op = ...

In another cell:

with tf.Session() as sess: # OR: with sv.managed_sess() as sess, etc.

    # Run training steps here ...
    sess.run(train_op)

I've noticed it takes time to create the graph in the first approach. Sometimes I'm using multiple graphs and the second approach is my only option. Why would one approach be better than the other?

2 Answers
Related