访问具有绝对范围的变量
Accessing variables with absolute scopes
我一直在研究 tensorflow docs 使用绝对名称而不是现有范围的相对名称检索变量的方法
类似 get_variable_absolute
的东西会收到 var 的绝对路径(即:h1/Weights
而不是 h1
变量范围内的 Weights
)
这个问题是出于对 this problem 的极度沮丧。
我在深入阅读 TensorFlow 的 tutorial on Sharing Variables 后找到了答案。
假设:
- 您在范围 'h1'
中创建了一个变量 'Weights'
- 你在范围内'foo'
- 您要检索变量 'h1/Weights'
为此,您需要保存使用 tf.variable_scope('h1')
创建的范围对象,以便在范围 'foo'.
中使用它
部分代码会更多eloquent:
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对象时,不仅是它的名字,你可以离开当前范围。
我一直在研究 tensorflow docs 使用绝对名称而不是现有范围的相对名称检索变量的方法
类似 get_variable_absolute
的东西会收到 var 的绝对路径(即:h1/Weights
而不是 h1
变量范围内的 Weights
)
这个问题是出于对 this problem 的极度沮丧。
我在深入阅读 TensorFlow 的 tutorial on Sharing Variables 后找到了答案。
假设:
- 您在范围 'h1' 中创建了一个变量 'Weights'
- 你在范围内'foo'
- 您要检索变量 'h1/Weights'
为此,您需要保存使用 tf.variable_scope('h1')
创建的范围对象,以便在范围 'foo'.
部分代码会更多eloquent:
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对象时,不仅是它的名字,你可以离开当前范围。