【问题标题】:Can't access TensorFlow Adam optimizer namespace无法访问 TensorFlow Adam 优化器命名空间
【发布时间】:2017-06-08 16:19:04
【问题描述】:

我正在尝试了解 GAN,我正在通过 the example here 工作。

下面使用 Adam 优化器的代码给出了错误

“ValueError: 变量 d_w1/Adam/ 不存在,或者不是用 tf.get_variable() 创建的。你的意思是在 VarScope 中设置 reuse=None 吗?”

我正在使用 TF 1.1.0

d_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=Dx, labels=tf.fill([batch_size, 1], 0.9)))
d_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=Dg, labels=tf.zeros_like(Dg)))
d_loss = d_loss_real + d_loss_fake

tvars = tf.trainable_variables()

d_vars = [var for var in tvars if 'd_' in var.name]
g_vars = [var for var in tvars if 'g_' in var.name]



# Train the discriminator
# Increasing from 0.001 in GitHub version
with tf.variable_scope(tf.get_variable_scope(), reuse=False) as scope:

    # Next, we specify our two optimizers. In today’s era of deep learning, Adam seems to be the
    # best SGD optimizer as it utilizes adaptive learning rates and momentum. 
    # We call Adam's minimize function and also specify the variables that we want it to update.
    d_trainer_real = tf.train.AdamOptimizer(0.0001).minimize(d_loss_real, var_list=d_vars)
    d_trainer_fake = tf.train.AdamOptimizer(0.0001).minimize(d_loss_fake, var_list=d_vars)

我认为 Adam 优化器将变量带入它自己的命名空间,但由于某种原因它们没有被初始化。我稍后在代码中调用global_variables_initializer,可以在 github 页面上看到。我正在检查文档,我认为这可能与我必须在那里打某种reuse_variables() 电话有关,但我不确定。

非常感谢任何帮助。

【问题讨论】:

  • 我不确定这个例子是不是最好的例子,因为它使用了两个优化器作为鉴别器。它可以使用已经定义的 d_loss 来使用单个优化器,从而可能完全避免这个问题。
  • 此外,鉴别器输出激活是线性的,它应该是一个 sigmoid(或其他范围从 0 到 1 的东西)。这就解释了为什么判别器分类在 0 时间步的结果中为负。
  • @jasekp 那么你有什么建议呢?拥有一个鉴别器优化器仍然会给我错误。我想让这个工作并理解 Adam 优化器,因为我认为这就是问题所在。之后我总是可以查看更多示例吗?知道如何让它工作吗?谢谢!
  • This implementation 更加简洁,并且在 MNIST 上给出了可靠的结果。

标签: machine-learning tensorflow convolution


【解决方案1】:

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

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

实际上,代码“reuse=False”并没有像您预期的那样工作。重用状态一旦设置为 True 就不能永远变回 False,重用状态将被其所有子作用域继承。

with tf.variable_scope(tf.get_variable_scope(), reuse=False) as scope:
    assert tf.get_variable_scope().reuse == True

我猜您在邮政编码之前的某处已将重用设置为 True,因此默认 variable_scope.reuse==True。然后为 Adam 创建一个新的 variable_scope,但是,新的 scope 会继承默认 scope 的重用状态。然后,Adam 在 state reuse==True 下创建变量,这会引发错误。

解决办法是在设置variable_scope.reuse=True的时候,在graph的默认范围下增加一个子范围,那么默认的scope.reuse还是False,Adam.minimize就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-02
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    • 2017-05-23
    • 1970-01-01
    • 2020-04-23
    相关资源
    最近更新 更多