TensorFlow
tf.get_default_session
session management
Python
machine learning

Tensorflow 'tf.get_default_session after sesstf.Session is None

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In TensorFlow 1.x, one of the most common sources of confusion is calling tf.get_default_session() right after creating a session with sess = tf.Session() and getting None back. This happens because creating a session and registering it as the default session are two separate operations. Understanding this distinction is essential for writing correct TF1 code and for migrating to TF2, where sessions are no longer needed.

Session vs. Default Session

A tf.Session object manages resources (CPU, GPU memory) and executes operations in a computational graph. However, simply creating one does not make it the "default" session. The default session is a thread-local reference that certain convenience functions (like Tensor.eval() and Operation.run()) look up automatically.

python
1import tensorflow as tf
2
3# TF1-style code
4sess = tf.Session()
5
6# This returns None -- sess exists, but it is not the default
7print(tf.get_default_session())  # None
8
9# You must explicitly run operations through sess
10result = sess.run(tf.constant(42))
11print(result)  # 42
12
13sess.close()

The key takeaway: tf.Session() allocates resources, but it does not register itself as the default session. That is why tf.get_default_session() returns None.

Making a Session the Default with as_default()

To register a session as the default, use its as_default() context manager. Inside the with block, tf.get_default_session() returns that session, and convenience methods like .eval() work without passing the session explicitly.

python
1import tensorflow as tf
2
3a = tf.constant(10)
4b = tf.constant(32)
5c = a + b
6
7sess = tf.Session()
8
9with sess.as_default():
10    print(tf.get_default_session() is sess)  # True
11    print(c.eval())                           # 42
12
13# Outside the block, the default is gone again
14print(tf.get_default_session())  # None
15
16sess.close()

You can also combine session creation and default registration using tf.Session() directly as a context manager. In this form the session is both the default and automatically closed when the block exits:

python
1import tensorflow as tf
2
3a = tf.constant(5)
4b = tf.constant(7)
5
6with tf.Session() as sess:
7    print(tf.get_default_session() is sess)  # True
8    print((a * b).eval())                     # 35
9
10# sess is closed here and default session is None

InteractiveSession

tf.InteractiveSession is a convenience class designed for notebooks and REPLs. Unlike tf.Session, it automatically registers itself as the default session upon creation:

python
1import tensorflow as tf
2
3sess = tf.InteractiveSession()
4
5# No as_default() needed
6print(tf.get_default_session() is sess)  # True
7
8x = tf.constant(100)
9print(x.eval())  # 100
10
11sess.close()

This is why many TensorFlow tutorials written for Jupyter notebooks use InteractiveSession -- it removes the need for the with block. However, in production code, explicit session management with tf.Session() and as_default() is preferred because it makes the scope of the default session clear.

Migrating to TensorFlow 2

TensorFlow 2 removes the session concept entirely. Operations execute eagerly by default, so there is no graph to "run" and no default session to manage:

python
1import tensorflow as tf  # TF2
2
3a = tf.constant(10)
4b = tf.constant(32)
5c = a + b
6
7print(c.numpy())  # 42 -- no session needed

If you are maintaining legacy TF1 code inside a TF2 environment, you can use the compatibility module:

python
1import tensorflow.compat.v1 as tf
2tf.disable_v2_behavior()
3
4# Now TF1 session APIs work as before
5with tf.Session() as sess:
6    print(sess.run(tf.constant(42)))  # 42

The long-term goal should be to rewrite TF1 session-based code to use TF2 eager execution and tf.function for graph optimization.

Common Pitfalls

  • Expecting tf.Session() to set the default session. This is the root cause of the None surprise. Always use as_default() or the with tf.Session() as sess: pattern if you need .eval() and .run() convenience methods.
  • Forgetting to close sessions. Each tf.Session allocates GPU memory and other resources. If you create sessions without closing them (or without using a with block), you will leak resources and eventually run out of memory.
  • Using InteractiveSession in production code. It implicitly sets the default, which can conflict with other sessions or make the code harder to reason about. Reserve it for exploratory work in notebooks.
  • Nesting as_default() contexts without understanding the stack. TensorFlow maintains a stack of default sessions. Nesting with sess1.as_default(): with sess2.as_default(): is legal, but the inner session shadows the outer one. Exiting the inner block restores the outer default. Mismanaging this stack leads to operations running on the wrong session.
  • Mixing TF1 session code with TF2 eager mode. Running tf.Session() in a TF2 environment without tf.compat.v1 and disable_v2_behavior() raises errors or produces confusing behavior. Decide on one execution model per module and stick with it.

Summary

  • tf.Session() creates a session but does not make it the default -- that is why tf.get_default_session() returns None.
  • Use sess.as_default() or with tf.Session() as sess: to register a session as the default within a scope.
  • tf.InteractiveSession automatically sets itself as the default, which is convenient for notebooks but not recommended for production.
  • TensorFlow 2 removes sessions entirely in favor of eager execution; use tf.compat.v1 if you must run legacy TF1 code.
  • Always close sessions (preferably with a with block) to avoid resource leaks.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.