gpt4 book ai didi

python - Tensorflow: 'tf.get_default_session()` 在 sess=tf.Session() 为 None 之后

转载 作者:太空狗 更新时间:2023-10-29 21:30:31 25 4
gpt4 key购买 nike

我试图找出为什么 tf.get_default_session() 总是返回 None 类型:

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

我不知道为什么 default = tf.get_default_session()None 因为我认为它应该返回上一个 session 。谁能弄清楚我的代码有什么问题?

最佳答案

只是创建一个 tf.Session() 并不能使其成为默认值。这基本上是 tf.Session 之间的区别和 tf.InteractiveSession :

sess = tf.InteractiveSession()
print(tf.get_default_session()) # this is not None!

tf.InteractiveSession 不同,tf.Session 仅在 with block (它是上下文管理器)内成为默认值:

sess = tf.Session()
with sess:
print(tf.get_default_session()) # this is not None!

关于python - Tensorflow: 'tf.get_default_session()` 在 sess=tf.Session() 为 None 之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47721792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com