I was trying to figure out why tf.get_default_session()
always returns None
type:
import tensorflow as tf
tf.reset_default_graph()
init=tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
default = tf.get_default_session()
default == None # True
I do not know why default = tf.get_default_session()
is None
since I thought it should return the previous session.
Could anyone figures out what's wrong with my code?
A session. run() call is almost like a function call: You specify the inputs and the function to be called, and you get back a set of outputs. In TensorFlow 2.0, you can decorate a Python function using tf. function() to mark it for JIT compilation so that TensorFlow runs it as a single graph (Functions 2.0 RFC).
In tensorflow, we often use sess. run() to call operations or calculate the value of a tensor.
Just creating a tf.Session()
doesn't make it a default. This is basically the difference between tf.Session
and tf.InteractiveSession
:
sess = tf.InteractiveSession()
print(tf.get_default_session()) # this is not None!
Unlike tf.InteractiveSession
, a tf.Session
becomes a default only inside with
block (it's a context manager):
sess = tf.Session()
with sess:
print(tf.get_default_session()) # this is not None!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With