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.
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.
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.
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:
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:
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:
If you are maintaining legacy TF1 code inside a TF2 environment, you can use the compatibility module:
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 theNonesurprise. Always useas_default()or thewith tf.Session() as sess:pattern if you need.eval()and.run()convenience methods. - Forgetting to close sessions. Each
tf.Sessionallocates GPU memory and other resources. If you create sessions without closing them (or without using awithblock), you will leak resources and eventually run out of memory. - Using
InteractiveSessionin 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. Nestingwith 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 withouttf.compat.v1anddisable_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 whytf.get_default_session()returnsNone.- Use
sess.as_default()orwith tf.Session() as sess:to register a session as the default within a scope. tf.InteractiveSessionautomatically 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.v1if you must run legacy TF1 code. - Always close sessions (preferably with a
withblock) to avoid resource leaks.
Related reading
- TensorFlow tf.image functions on a 4D image batch
- Tensorflow tf.layers.batch_normalization doesn't add update ops to tf.GraphKeys.UPDATE_OPS
- Tensorflow tf.losses.cosine_distance is greater than one
- Tensorflow TFRecord Can't parse serialized example
- TensorFlow tf.summary.text and linebreaks
- Tensorflow The channel dimension of the inputs should be defined
- Tensorflow, try and except doesn''t handle exception
- Tensorflow TypeError expected bytes, Descriptor found
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.