【问题标题】:How can you re-use a variable scope in tensorflow without a new scope being created by default?如何在不默认创建新范围的情况下在 tensorflow 中重用变量范围?
【发布时间】:2018-08-23 10:01:19
【问题描述】:

我在图表的一部分中创建了一个变量范围,稍后在图表的另一部分中,我想将 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

【问题讨论】:

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


    【解决方案1】:

    这是一种在上下文管理器中使用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,您仍然会得到相同的结果。

    【讨论】:

    • var2 不在myscope 之下,我错过了什么吗?
    • 你是否知道在这个例子中是否有办法让我查询 ms1 的 tensorflow?在我的实际使用中,我没有创建原始变量范围,它是在 tensorflow 中为我创建的,我实际上只是想向该范围添加一些变量。这是优化器实现的一部分,并且 tensorflow 以一种框架未针对我的用例的一个细节处理的方式处理一些范围。如果不放心,还有另一种处理方法。
    • 啊,你可以用tf.variable_scope('myscope/'):代替tf.variable_scope(ms1.original_name_scope)我不知道使用'/'是必要的。现在一切都说得通了!再次感谢。
    【解决方案2】:

    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]))
    

    这个sn-p会输出:

    <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>
    

    【讨论】:

      猜你喜欢
      • 2015-10-28
      • 1970-01-01
      • 2019-11-27
      • 2011-07-25
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      相关资源
      最近更新 更多