1

我使用版本为 2.2.4 的 keras 在 CelebA 数据集上训练 DCGAN。我对 keras 功能 API 实现感到困惑,例如预测DCGAN 架构中的 BN 层。

在训练过程中,我需要使用generator.predict(noise)来获取一些用于判别器训练的假图像。并且我需要在训练过程中抽取样本来显示网络是否也在使用generator.predict(noise)进行改进。我知道在鉴别器训练期间,生成器生成的假图像应该在训练模式下通过生成器中的 BN 层,而从生成器中抽取样本时,应该在测试模式下完成。因为我在这两种情况下都使用预测方法,我想知道如何确保在该方法中正确使用训练和测试模式。任何人都可以告诉我我是否完成了训练,当我使用时BN层处于哪种模式预测方法。谢谢。

代码在一次迭代中是这样的:

noise = np.random.normal(0, 1, size=(batch_size, noise_dims))
fake_batch = generator.predict(noise) # are the BN layers in training mode?
real_batch = data_loader.load_data(batch_size)
# train discriminator
discriminator.train_on_batch(real_batch, np.ones(batch_size)*0.9)
discriminator.train_on_batch(fake_batch, np.zeros(batch_size))
# train generator
discriminator.trainable = False
noise = np.random.normal(0, 1, size=(batch_size, noise_dims))
combine_model.train_on_batch(noise, np.ones(batch_size)) 
# combine_model is built by Model(noise, discriminator(generator(noise)))
# I also wonder whether this generator and discriminator call are in training mode

if iter % sample_interval == 0:
    noise = np.random.normal(0, 1, size=(sample_size, noise_dims))
    generator.predict(noise)
    # are the BN layers run in test mode?


想知道我对 DCGAN 中 BN 层的理解是否正确,以及keras 中的predict方法如何处理 BN 或 dropout 中的训练或测试模式。最后如果可能的话,如何在使用 keras 时手动控制 DCGAN 训练中的模式(set_learning_phase?)。

4

0 回答 0