python - 我们应该如何处理 name_scope 的自增呢?
问题描述
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/
s?
当我想让我的 tf 模型成为 webapp 时,这是一个问题。该应用程序可以正确处理第一个请求。但是当涉及到第二个或以后的请求时,它会尝试将图形以及所有变量与另一个name_scope
(例如,foo_1
)一起加载,并导致致命错误。
解决方案
您可以存储该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/
推荐阅读
- javascript - 使用 jQuery 隐藏/显示元素
- java - 如何在 Spring Boot 中公开诸如可下载链接之类的资源
- uwp - ReactiveUI - 如何共享属性?
- ios - 如何确定是否启用了 ASAM?
- javascript - 给定特定输入时如何重定向到特定页面
- scala - 找不到参数解析器的隐式值:play.api.mvc.BodyParser[Any]
- bash - JMeter Beanshell 断言:如何保存 Runtime.getRuntime().exec("ifconfig | grep \"${IpAddress}\" | wc -l") 的值?
- java - 检测服务器套接字关闭
- python - 用唯一列表中的后续值替换列值
- ios - 如何禁用 ScrollView 项目上的多个按钮颜色选择?