【问题标题】:variable_scope causes 'variable does not exist' with Optimizervariable_scope 导致优化器出现“变量不存在”
【发布时间】:2017-03-05 02:15:01
【问题描述】:

假设我有两个模型 foobar。假设 bar 已经过预训练和加载。我想为foo 定义成本函数,大致如下代码所示(它实际上是一个自动编码器)。请注意,这是重现我的问题的最小示例,因此它们在数学上没有意义。

import tensorflow as tf

def foo(X):
        with tf.variable_scope("foo"):
                A = tf.get_variable("A",shape=[1])
        return tf.add(X,A)

def bar(X):
        with tf.variable_scope("bar"):
                B = tf.get_variable("B",shape=[1])
        return tf.multiply(X,B)

X = tf.placeholder("float")

X_prime = foo(X)

Y = bar(X)
tf.get_variable_scope().reuse_variables()
Y_prime = bar(X_prime)


#foo(X) is manipulated with some other terms, but the point is foo is called again
cost = foo(X) + tf.pow(Y-Y_prime,2) 

optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost)

如果我运行脚本(TF 1.0 版),我会收到以下错误:

ValueError: Variable foo/A/Adam/ does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?

但是,GradientDescentOptimizer 不会发生这种情况。任何解释和指针将不胜感激。

【问题讨论】:

  • 有人可以帮忙吗?
  • 您的问题解决了吗?在我看来,在全局设置tf.get_variable_scope().reuse_variables() 之后,该行之后的所有变量都会查找现有变量。如果没有这个变量,tensorflow会提示那个错误。像 Momentum、Adam 这样的优化器,当他们尝试minimize 成本时,他们需要创建变量来存储历史梯度。解决此问题的一种可能方法是,您可以通过为函数添加参数来在本地设置variable_scope("foo", reuse=reuse),而不是全局设置。

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


【解决方案1】:

您的 ValueError 是由在 variable_scope.reuse==True 中创建新变量引起的。

当您调用 Adam 的最小化函数时,变量由 Adam 创建,用于保存图中每个可训练变量的动量。

您已将重用设置为 True,因此默认 variable_scope.reuse==True。重用状态一旦设置为 True,就不能永远变回 False。然后,Adam 在 state reuse==True 下创建变量,这会引发错误。

解决方法是在设置variable_scope.reuse=True时,在graph的默认范围下增加一个子范围,那么默认的scope.reuse还是False,Adam.minimize就起作用了,如下:

import tensorflow as tf
def foo(X):
        with tf.variable_scope("foo"):
                A = tf.get_variable("A",shape=[1])
        return tf.add(X,A)

def bar(X):
        with tf.variable_scope("bar"):
                B = tf.get_variable("B",shape=[1])
        return tf.multiply(X,B)

X = tf.placeholder("float")

with tf.variable_scope("for_reuse_scope"):
    X_prime = foo(X)
    Y = bar(X)
    tf.get_variable_scope().reuse_variables()
    Y_prime = bar(X_prime)


#foo(X) is manipulated with some other terms, but the point is foo is called again
cost = foo(X) + tf.pow(Y-Y_prime,2) 

optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost)

【讨论】:

  • 您需要将:with tf.variable_scope(tf.get_variable_scope()) 放在在您的设备上运行的循环前面......所以,这样做:with tf.variable_scope(tf.get_variable_scope ()): for i in xrange(FLAGS.num_gpus): with tf.device('/gpu:%d' % i):
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-29
  • 1970-01-01
  • 2017-08-29
  • 2014-05-21
  • 2016-04-01
  • 2018-08-07
  • 1970-01-01
相关资源
最近更新 更多