gpt4 book ai didi

python - 为什么在 Tensorflow 中使用和不使用上下文管理器定义 tf.Session 会导致不同的行为?

转载 作者:行者123 更新时间:2023-12-02 11:43:59 26 4
gpt4 key购买 nike

我注意到,使用和不使用上下文管理器定义 session 时存在差异。这里有一个例子:

使用上下文管理器:

import tensorflow as tf

graph = tf.Graph()
with graph.as_default():
x = tf.Variable(0)
tf.summary.scalar("x", x)

with tf.Session(graph=graph) as sess:
summaries = tf.summary.merge_all()
print("Operations:", sess.graph.get_operations())
print("\nSummaries:", summaries)

结果:

Operations: [<tf.Operation 'Variable/initial_value' type=Const>, <tf.Operation 'Variable' type=VariableV2>, <tf.Operation 'Variable/Assign' type=Assign>, <tf.Operation 'Variable/read' type=Identity>, <tf.Operation 'x/tags' type=Const>, <tf.Operation 'x' type=ScalarSummary>, <tf.Operation 'Merge/MergeSummary' type=MergeSummary>]

Summaries: Tensor("Merge/MergeSummary:0", shape=(), dtype=string)

没有上下文管理器:

import tensorflow as tf

graph = tf.Graph()
with graph.as_default():
x = tf.Variable(0)
tf.summary.scalar("x", x)

sess = tf.Session(graph=graph)
summaries = tf.summary.merge_all()
print("Operations:", sess.graph.get_operations())
print("Summaries:", summaries)
sess.close()

结果:

Operations: [<tf.Operation 'Variable/initial_value' type=Const>, <tf.Operation 'Variable' type=VariableV2>, <tf.Operation 'Variable/Assign' type=Assign>, <tf.Operation 'Variable/read' type=Identity>, <tf.Operation 'x/tags' type=Const>, <tf.Operation 'x' type=ScalarSummary>]
Summaries: None

为什么tf.summary.merge_all()找不到摘要?

最佳答案

您可以找到tf.summary.merge_all()的实现here 。它的工作原理是调用 this function ,它从 get_default_graph() 返回的图表中获取集合。该函数的文档如下:

"""Returns the default graph for the current thread.

The returned graph will be the innermost graph on which a
`Graph.as_default()` context has been entered, or a global default
graph if none has been explicitly created.

NOTE: The default graph is a property of the current thread. If you
create a new thread, and wish to use the default graph in that
thread, you must explicitly add a `with g.as_default():` in that
thread's function.

Returns:
The default `Graph` being used in the current thread.
"""

因此,在没有 session 上下文管理器的代码中,问题不一定是因为您不在 session 中;而是因为您没有 session 上下文管理器。问题是带有摘要的图表不是默认图表,并且您尚未输入该图表的上下文(如 session )。

有几种不同的方法可以“解决”这个问题,而无需使用 with tf.Session(graph=graph) as sess: 上下文管理器:

<小时/>

一种选择是将摘要合并在一起,同时您仍然将 graph 作为默认图表:

import tensorflow as tf

graph = tf.Graph()
with graph.as_default():
x = tf.Variable(0)
tf.summary.scalar("x", x)
summaries = tf.summary.merge_all()

with tf.Session(graph=graph) as sess:
print("Operations:", sess.graph.get_operations())
print("\nSummaries:", summaries)
<小时/>

另一种选择是在合并摘要之前显式__enter__() session (这与with tf.Session(graph=graph)中Python内部发生的情况几乎相同as sess: 语句):

import tensorflow as tf

graph = tf.Graph()
with graph.as_default():
x = tf.Variable(0)
tf.summary.scalar("x", x)

sess = tf.Session(graph=graph)
sess.__enter__()
summaries = tf.summary.merge_all()
print("Operations:", sess.graph.get_operations())
print("Summaries:", summaries)
sess.close()

关于python - 为什么在 Tensorflow 中使用和不使用上下文管理器定义 tf.Session 会导致不同的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49465549/

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