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, creating a session with sess = tf.Session() does not automatically make it the default session. A default session exists only inside an active session context such as with sess.as_default():, which is why tf.get_default_session() can still return None even though you already created a session object.
Why tf.get_default_session() Returns None
TensorFlow 1.x keeps a thread-local stack of default sessions. Assigning a session to a Python variable does not push it onto that stack.
This means:
The session exists, and you can run tensors through it explicitly, but TensorFlow has no active default session in the current context.
Use sess.as_default()
If you want APIs that rely on the default session, enter a default-session context:
Inside that with block, tf.get_default_session() returns the session you activated.
Why .eval() Often Triggers the Confusion
Many TensorFlow 1.x examples use .eval() on tensors:
But .eval() depends on there being a default session. If you did not enter a with sess.as_default(): block, it will fail even though sess.run(a) works.
That difference is what confuses many people:
- '
sess.run(tensor)uses the explicit session you pass' - '
tensor.eval()expects a default session to already exist'
Graph and Session Scope Must Match
In TensorFlow 1.x, sessions are bound to graphs. If you are working with multiple graphs, the default graph and default session must line up:
Mixing tensors from one graph with a session created for another graph produces opaque runtime errors.
The Safer Pattern: Pass the Session Explicitly
Legacy TensorFlow code is usually easier to reason about when functions receive the session explicitly:
This avoids hidden dependencies on global default session state.
Modern TensorFlow Usually Does Not Use Sessions
TensorFlow 2 uses eager execution by default, so most new code does not need session APIs at all:
If you are maintaining TensorFlow 1.x-style code under TensorFlow 2, you may still encounter tf.compat.v1.Session() and related compatibility APIs. But for new code, session management is usually legacy behavior, not the recommended model.
Clean Up Sessions
Sessions own runtime resources, so close them or use a context manager:
That pattern prevents resource leaks in loops and training scripts.
Common Pitfalls
- Expecting
sess = tf.Session()to automatically define the default session. - Calling
.eval()outside awith sess.as_default():context. - Mixing graph scope and session scope incorrectly.
- Hiding session dependencies in helpers instead of passing the session explicitly.
- Writing new TensorFlow 2 code as if TensorFlow 1.x session patterns were still the normal default.
Summary
- In TensorFlow 1.x, creating a session and making it the default are different things.
- '
tf.get_default_session()returnsNoneuntil a default-session context is active.' - Use
with sess.as_default():when code depends on a default session. - Prefer explicit
sess.run(...)or explicit session parameters in legacy code. - In TensorFlow 2, sessions are mostly legacy compatibility tools rather than the standard programming model.
Related reading
- Tensorflow 'tf.get_default_session after sesstf.Session is None
- 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, 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.