Calculate GAN loss for the discriminator Parameters: netD (network) -- the discriminator D real (tensor array) -- real images fake (tensor array) -- images generated by a generator Return the discriminator loss. We also call loss_D.ba
(self, netD, real, fake)
| 169 | self.idt_B = self.netG_B(self.real_A) |
| 170 | |
| 171 | def backward_D_basic(self, netD, real, fake): |
| 172 | """Calculate GAN loss for the discriminator |
| 173 | Parameters: |
| 174 | netD (network) -- the discriminator D |
| 175 | real (tensor array) -- real images |
| 176 | fake (tensor array) -- images generated by a generator |
| 177 | |
| 178 | Return the discriminator loss. |
| 179 | We also call loss_D.backward() to calculate the gradients. |
| 180 | """ |
| 181 | # Real |
| 182 | pred_real = netD(real) |
| 183 | loss_D_real = self.criterionGAN(pred_real, True) |
| 184 | # Fake |
| 185 | pred_fake = netD(fake.detach()) |
| 186 | loss_D_fake = self.criterionGAN(pred_fake, False) |
| 187 | # Combined loss and calculate gradients |
| 188 | loss_D = (loss_D_real + loss_D_fake) * 0.5 |
| 189 | loss_D.backward() |
| 190 | return loss_D |
| 191 | |
| 192 | def backward_D_A(self): |
| 193 | """Calculate GAN loss for discriminator D_A""" |
no test coverage detected