【发布时间】:2017-03-05 02:15:01
【问题描述】:
假设我有两个模型 foo 和 bar。假设 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