2017-06-08 22 views
2

我正在尝试了解GAN,并正在通过the example here进行工作。无法访问TensorFlow Adam优化程序名称空间

下面使用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) 

错误我认为亚当优化走的是变量纳入自己的命名空间但由于某些原因,他们没有初始化。我在代码中稍后调用global_variables_initializer,正如可以在github页面上看到的那样。我正在通过文档检查,我认为这可能与我不得不在某处放置某种reuse_variables()电话相关,但我不确定。

任何帮助非常感谢。

+0

我不知道,这个例子是按照最好的一个,考虑到它使用两个优化的鉴别。它可以使用已经定义的d_loss来使用单个优化器,可能完全避免这个问题。 – jasekp

+0

此外,鉴别器输出激活是线性的,它应该是一个sigmoid(或其他范围从0到1的其他东西)。这解释了为什么鉴别器分类在0时步结果为负的原因。 – jasekp

+0

@jasekp那么你会建议什么?有一个鉴别器优化器仍然给我错误。我想得到这个工作,并理解亚当优化器,因为我认为这是什么问题在这里。我可以随时查看更多示例吗?任何想法如何让它工作?谢谢! –

回答

0

你ValueError异常是由variable_scope.reuse ==真中创建新的变量引起的。

变量由亚当创建的,当你调用亚当的最小化功能,保存每一个可训练变量的势头在图形中。

其实,你所期望的代号为“重用=假”不起作用。一旦将其设置为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,然而,新的作用域将继承默认作用域的重用状态。然后,Adam在状态复用== True的情况下创建变量,这会引发错误。

解决的办法是在设置variable_scope.reuse = True时,在图的默认范围下添加一个子范围,然后默认的scope.reuse仍然是False,并且Adam.minimize将起作用。