- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我想要一段代码,如果它不存在,则在范围内创建一个变量,如果它已经存在,则访问该变量。我需要它是 same 代码,因为它将被多次调用。
但是,Tensorflow 需要我指定是要创建还是重用变量,如下所示:
with tf.variable_scope("foo"): #create the first time
v = tf.get_variable("v", [1])
with tf.variable_scope("foo", reuse=True): #reuse the second time
v = tf.get_variable("v", [1])
如何让它确定是自动创建还是重用它?即,我希望上面的两个代码块是 same 并让程序运行。
最佳答案
get_variable()
在创建新变量且未声明形状时,或在创建变量期间违反重用时会引发 ValueError
。因此,你可以试试这个:
def get_scope_variable(scope_name, var, shape=None):
with tf.variable_scope(scope_name) as scope:
try:
v = tf.get_variable(var, shape)
except ValueError:
scope.reuse_variables()
v = tf.get_variable(var)
return v
v1 = get_scope_variable('foo', 'v', [1])
v2 = get_scope_variable('foo', 'v')
assert v1 == v2
请注意,以下内容也有效:
v1 = get_scope_variable('foo', 'v', [1])
v2 = get_scope_variable('foo', 'v', [1])
assert v1 == v2
更新。新的 API 现在支持自动重用:
def get_scope_variable(scope, var, shape=None):
with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
v = tf.get_variable(var, shape)
return v
关于python - TensorFlow 变量范围 : reuse if variable exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38545362/
我在 WebApi 项目中使用 DryIoc。默认重用设置为 Reuse.InWebRequest。在某些情况下,我需要在请求期间解析新实例。使用 Reuse.InCurrentScope 时,我可以
问题:当 Collection View 单元格滚动到屏幕外并重新使用时,对 Collection View 单元格的引用会发生什么情况?我在不使用“出队”的情况下提供细胞的所有尝试都失败了。有没有办
有什么想法可以解决如下所示的问题吗?根据我在网上找到的信息,它与重用 tensorflow 范围的问题有关,但没有任何效果。 ValueError: Variable rnn/basic_rnn_ce
去年,我参加了C++的数据结构类(class),因此以模板代码实现了所有主要的数据结构。我将所有内容都保存在闪存驱动器中,因为我觉得在生活中的某个时刻,我会再次使用它。我想我最终编程的东西将需要B树,
几年前,有人告诉我一项关于代码重用的研究。显然,发现程序员在搜索要重用的代码时平均有 7 分钟的时间。如果他们在那个窗口中没有找到适合他们需要的代码,他们就会编写自己的代码。 这是在需要仔细管理代码以
我一直是一个糟糕的程序员,因为我正在做复制和粘贴。一个例子是,每次我连接到数据库并检索记录集时,我都会复制以前的代码并编辑,复制设置 datagridview 的代码并进行编辑。我知道短语代码重用,但
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
关闭。这个问题需要多问focused 。目前不接受答案。 已关闭 3 年前。 已锁定。这个问题及其答案是locked因为这个问题是题外话,但却具有历史意义。目前不接受新的答案或互动。 任何代码都可以以
我通常会维护代码片段,我可以在 Wikidpad 个人 wiki 中重新使用这些代码片段,并带有一个小索引页面,可以将我带到我想要的任何代码。 我想与我的团队分享这些片段,并正在寻找简单的方法来做到这
我有以下路线,id 是一个可选参数,因此用户可以加载信息,或者如果没有提供 id,他们可以添加信息。我希望能够重用组件,这样页面就不必重新加载(在 ngOnInit 中调用 api),但是因为它们是两
我想对多个数据表应用相同的聚合,而不需要重写聚合方案。 考虑 dt1 <- data.table(id = c(1,2), a = rnorm(10), b = rnorm(10), c = rnor
我的公司有两到三个使用大量通用代码的 Web 应用程序——自定义 MVC 框架、实用程序类、JavaScript 库等等。 我们不想在每个应用程序中复制所有这些代码,因为我们最终会使用它的几个略有不同
许多个月前,我开始将我的音乐存储为 MP3。我疯狂地下载,然后将它们全部转储到一个文件夹中。在收集了数千首歌曲后,我一团糟。在我的空闲时间整理了所有音乐两年后,我已经把它放到了我图书馆的“D”部分。我
我正在尝试学习specflow,现在。 目前我有 2 个功能文件。 在第二个功能文件中,我重用了第一个功能文件中的一个步骤。 Specflow 自动识别第一个功能文件中的步骤,当 Specflow 为
有时我会偶然发现我想表达“请使用最后一个参数两次”的问题,例如为了编写无点样式或避免使用 lambda。例如。 sqr x = x * x 可以写成 sqr = doubleArgs (*) wher
这个问题已经有答案了: Javascript: how to pass found string.replace value to function? (3 个回答) 已关闭 7 年前。 我有一个功能
我想查找字符串中出现的字符(如果它们出现在某个“标记”字符之前或之后)。 所以我的标记是-,字符串是“a b-c d”。我想按顺序查找 b 和 c。 我试过了 re.findall(r'(-[a-z]
这个问题在这里已经有了答案: How to set variable reuse back to False in Tensorflow? (2 个答案) 关闭 6 年前。 有没有办法取消设置变量范
我会尽量简明扼要.. 我目前正在开发一个小型的实时“MMO”,并希望减少主机 -> 客户端的带宽使用。 其中成本最高的是字符串的使用,在大多数情况下,字符串会不断重复并发送给客户端。由于它们的性质(无
我读到 ThreadPoolExecutor 有线程池,这个池注定要降低创建新线程的成本(至少我是这样理解下面的短语): When you send a task to the executor, i
我是一名优秀的程序员,十分优秀!