2016-06-09 83 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对象的范围,而不仅仅是它的名字,就可以摆脱目前的范围。