如何在不默认创建新范围的情况下在 tensorflow 中重用变量范围? [英] How can you re-use a variable scope in tensorflow without a new scope being created by default?
问题描述
我在图表的一个部分创建了一个可变范围,稍后在图表的另一部分我想将 OP 添加到现有范围.这相当于这个蒸馏的例子:
I have created a variable scope in one part of my graph, and later in another part of the graph I want to add OPs to an existing scope. That equates to this distilled example:
import tensorflow as tf
with tf.variable_scope('myscope'):
tf.Variable(1.0, name='var1')
with tf.variable_scope('myscope', reuse=True):
tf.Variable(2.0, name='var2')
print([n.name for n in tf.get_default_graph().as_graph_def().node])
产生的结果:
['myscope/var1/initial_value',
'myscope/var1',
'myscope/var1/Assign',
'myscope/var1/read',
'myscope_1/var2/initial_value',
'myscope_1/var2',
'myscope_1/var2/Assign',
'myscope_1/var2/read']
我想要的结果是:
['myscope/var1/initial_value',
'myscope/var1',
'myscope/var1/Assign',
'myscope/var1/read',
'myscope/var2/initial_value',
'myscope/var2',
'myscope/var2/Assign',
'myscope/var2/read']
我看到这个问题似乎没有直接解决问题的答案:TensorFlow,如何重用变量作用域名称
I saw this question which didn't seem to have an answer that addressed the question directly: TensorFlow, how to reuse a variable scope name
推荐答案
这是在上下文管理器中使用 as
和 somename
来完成此操作的一种直接方法.使用此 somename.original_name_scope
属性,您可以检索该范围,然后向其中添加更多变量.下图为:
Here is one straightforward way to do this using as
with somename
in a context manager. Using this somename.original_name_scope
property, you can retrieve that scope and then add more variables to it. Below is an illustration:
In [6]: with tf.variable_scope('myscope') as ms1:
...: tf.Variable(1.0, name='var1')
...:
...: with tf.variable_scope(ms1.original_name_scope) as ms2:
...: tf.Variable(2.0, name='var2')
...:
...: print([n.name for n in tf.get_default_graph().as_graph_def().node])
...:
['myscope/var1/initial_value',
'myscope/var1',
'myscope/var1/Assign',
'myscope/var1/read',
'myscope/var2/initial_value',
'myscope/var2',
'myscope/var2/Assign',
'myscope/var2/read']
备注
另请注意,设置 reuse=True
是可选的;也就是说,即使您通过了 reuse=True
,您仍然会得到相同的结果.
Remark
Please also note that setting reuse=True
is optional; That is, even if you pass reuse=True
, you'd still get the same result.
另一种方法(感谢 OP 本人!)是在重用变量范围的末尾添加 /
,如下例所示:
Another way (thanks to OP himself!) is to just add /
at the end of the variable scope when reusing it as in the following example:
In [13]: with tf.variable_scope('myscope'):
...: tf.Variable(1.0, name='var1')
...:
...: # reuse variable scope by appending `/` to the target variable scope
...: with tf.variable_scope('myscope/', reuse=True):
...: tf.Variable(2.0, name='var2')
...:
...: print([n.name for n in tf.get_default_graph().as_graph_def().node])
...:
['myscope/var1/initial_value',
'myscope/var1',
'myscope/var1/Assign',
'myscope/var1/read',
'myscope/var2/initial_value',
'myscope/var2',
'myscope/var2/Assign',
'myscope/var2/read']
备注:
请注意,设置 reuse=True
也是可选的;也就是说,即使您通过了 reuse=True
,您仍然会得到相同的结果.
Remark:
Please note that setting reuse=True
is again optional; That is, even if you pass reuse=True
, you'd still get the same result.
这篇关于如何在不默认创建新范围的情况下在 tensorflow 中重用变量范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!