如何在没有默认创建新范围的情况下在tensorflow中重用变量范围?

时间:2018-03-14 18:21:19

标签: python tensorflow machine-learning deep-learning tensor

我在图表的一个部分创建了一个变量作用域,稍后在图形的另一部分我想要将OP添加到现有作用域。这相当于这个蒸馏的例子:

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, how to reuse a variable scope name

2 个答案:

答案 0 :(得分:3)

以下是在上下文管理器中使用assomename执行此操作的简单方法。使用此somename.original_name_scope属性,您可以检索该范围,然后向其中添加更多变量。以下是插图:

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,您仍然会得到相同的结果。

另一种方式(感谢OP本人!)只是在重用时在变量范围的末尾添加/,如下例所示:

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,您仍然会得到相同的结果。

答案 1 :(得分:0)

kmario23提到的答案是正确的,但在tf.get_variable创建的变量中存在一个棘手的情况:

with tf.variable_scope('myscope'):
    print(tf.get_variable('var1', shape=[3]))

with tf.variable_scope('myscope/'):
    print(tf.get_variable('var2', shape=[3]))

此代码段将输出:

<tf.Variable 'myscope/var1:0' shape=(3,) dtype=float32_ref>
<tf.Variable 'myscope//var2:0' shape=(3,) dtype=float32_ref>

看来tensorflow尚未提供处理这种情况的正式方法。我发现的唯一可能的方法是手动分配正确的名称(警告:不能保证正确性):

with tf.variable_scope('myscope'):
    print(tf.get_variable('var1', shape=[3]))

with tf.variable_scope('myscope/') as scope:
    scope._name = 'myscope'
    print(tf.get_variable('var2', shape=[3]))

然后我们可以获得正确的名称:

<tf.Variable 'myscope/var1:0' shape=(3,) dtype=float32_ref>
<tf.Variable 'myscope/var2:0' shape=(3,) dtype=float32_ref>