簡體   English   中英

如何在沒有默認創建新范圍的情況下在tensorflow中重用變量范圍?

[英]How can you re-use a variable scope in tensorflow without a new scope being created by default?

我在圖的一部分中創建了一個變量作用域,稍后在圖的另一部分中我想將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,如何重用變量范圍名稱

這里有一個簡單的方法來做到這一點使用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 ,你仍然會得到相同的結果。

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM