machine-learning - 无法访问 TensorFlow Adam 优化器命名空间

标签 machine-learning tensorflow convolution

我正在尝试了解 GAN,并且正在研究 the example here .

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

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

我使用的是 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() 调用有关,但我不确定。

非常感谢任何帮助。

最佳答案

您的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

我猜你已经在帖子代码之前的某个地方将reuse设置为True,因此默认的variable_scope.reuse==True。然后你为Adam创建一个新的variable_scope,但是,新的作用域将继承默认作用域的重用状态。然后,Adam 在状态reuse==True 下创建变量,这会引发错误。

解决方案是当设置variable_scope.reuse=True时,在图的默认作用域下添加一个子作用域,那么默认的scope.reuse仍然是False,而Adam.minimize将起作用。

关于machine-learning - 无法访问 TensorFlow Adam 优化器命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44440900/

相关文章:

signal-processing - 为什么我的卷积结果在使用 FFT 时会发生偏移

python - 如何加速随机梯度下降?

machine-learning - 使用 TensorFlow 对不平衡数据进行训练

python - 如何在 TensorFlow 中获取 None 的真实形状(动态输入形状)?

python - 为什么我的 keras LSTM 模型会陷入无限循环?

iphone - 如何提高 iPhone 上的 OpenCV 性能?

convolution - 如何计算 GoogLe Net 的参数数量?

algorithm - 小样本的最佳一类分类器

machine-learning - 自动编码器中的绑定(bind)权重

python - 如何在 GPU 中的 Tensorflow 上并行运行独立循环