akanimax / big-discriminator-batch-spoofing-gan

BMSG-GAN with more features
MIT License
45 stars 6 forks source link

Wrong calculation of LSGAN #4

Open Hzzone opened 4 years ago

Hzzone commented 4 years ago

There exists an apparent error in the implementation of LSGAN below.

https://github.com/akanimax/BBMSG-GAN/blob/da82aa2e8507d17801bd2134a4ae754335d716f5/sourcecode/MSG_GAN/Losses.py#L145

Right implementation:

class LSGAN(GANLoss):

    def __init__(self, dis):
        super().__init__(dis)

    def dis_loss(self, real_samps, fake_samps):
        real_scores = th.mean((self.dis(real_samps) - 1) ** 2)
        fake_scores = th.mean(self.dis(fake_samps) ** 2)
        return 0.5 * (real_scores + fake_scores)

    def gen_loss(self, _, fake_samps):
        return 0.5 * th.mean((self.dis(fake_samps) - 1) ** 2)

reference equation 8 of [Least Squares Generative Adversarial Networks].

gif

akanimax commented 4 years ago

@Hzzone,

Thanks for the issue. You are right. We didn't much experiment with LS-GAN, although what we have isn't too different. Since (a - b)^2 = a^2 -2ab + b^2; and expectation would be E[a^2] - 2E[ab] + E[b^2]. Now, in our case, b=1, hence we only get (E[a^2] - 2E[a] + 1). The only missing term is E[a]. So, because of the upper bound, the GAN should still train fine, but you are correct that this needs to be fixed. Would you please be kind to open a PR?

Thanks and cheers :beers:! @akanimax