1.问题
https://discuss.pytorch.org/t/why-dont-we-put-models-in-train-or-eval-modes-in-dcgan-example/7422
链接中问题当在生成时,如果将generator状态设置为eval(),那么就会模式坍缩,只生成一张图片。
回答:
GAN的训练非常不稳定,eval不如train模式下稳定,为了模型在eval的时候稳定,在结束训练时,需要先用一个minibatch做生成,这样bn层的mean和std都会稳定。
# Training phase G.train() D.train() for epoch in range(n_epoch): .... # train D and G # Now run generation for a few min-batch while keeping the mode as "train" for _ in range(additional_iters): z = ... # sample a z _ = G(z) # Now switch to eval mode to do the actual generation G.eval() D.eval() z = ... samples2output = G(z)
但eval的时候mean和std会有什么影响吗?
2.训练技巧
https://www.jianshu.com/p/aab68eb0f7ed
误差曲线走向:
理想的Generator loss的误差曲线应该是不断往0靠的下降的抛物线。
理想的Discrimnator loss的误差曲线应该是最终在0附近振荡,即傻傻分不清。换言之,就是判别器有50%的概率判断你是真的,50%概率判断你是假的。
https://zhuanlan.zhihu.com/p/188734829
- 在生成器损失使用梯度惩罚情况下,判别器尽量避免使用BN
- 避免梯度稀疏及信息丢失: