2016-06-09 82 views
0

我一直在調查tensorflow docs一些方法來檢索變量使用絕對名稱,而不是相對名稱現有範圍絕對範圍訪問變量

喜歡的東西get_variable_absolute,將收到的VAR的絕對路徑(即:h1/Weights而不是Weightsh1變量範圍內)

這個問題的動機是極端的沮喪與this problem

回答

4

我從TensorFlow深入閱讀tutorial on Sharing Variables後發現了答案。

假設:

  • 你創建了一個範圍「H1」
  • 你是在一個範圍內「富」
  • 要檢索的變量的變量'權重「H1 /重量」

爲此,您需要保存使用tf.variable_scope('h1')創建的作用域對象,以便在作用域'foo'中使用它。

一些代碼將更加雄辯:

with tf.variable_scope('h1') as h1_scope: # we save the scope object in h1_scope 
    w = tf.get_variable('Weights', []) 

with tf.variable_scope('foo'): 
    with tf.variable_scope(h1_scope, reuse=True): # get h1_scope back 
    w2 = tf.get_variable('Weights') 

assert w == w2 

結論:當你通過其Python對象的範圍,而不僅僅是它的名字,就可以擺脫目前的範圍。