gpt4 book ai didi

python - 我们应该如何处理name_scope的自动增量?

转载 作者:行者123 更新时间:2023-12-01 08:54:44 25 4
gpt4 key购买 nike

def foo():
with tf.Session() as sess:
with tf.name_scope("foo") as abso:
print(abso)

for i in range(10):
foo()

这是测试 tf 的 name_scope 的简单代码。该代码段的输出是

foo/
foo_1/
foo_2/
foo_3/
foo_4/
foo_5/
foo_6/
foo_7/
foo_8/
foo_9/

哪条代码的输出都是foo/

当我想让我的 tf 模型成为 web 应用程序时,这是一个问题。应用程序可以正确处理第一个请求。但是,当涉及第二个或更高版本的请求时,它会尝试使用另一个 name_scope(例如 foo_1)加载图表以及所有变量,这会导致 fatal error 错误。

最佳答案

您可以存储由 tf.name_scope 方法创建的范围并重用它。例如:

scope = "foo"
def foo():
global scope
with tf.Session() as sess:
with tf.name_scope(scope) as scope:
print(scope)
for i in range(10):
foo()

将打印

foo/
foo/
foo/
foo/
foo/
foo/
foo/
foo/
foo/
foo/

关于python - 我们应该如何处理name_scope的自动增量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52854618/

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