GAN Losses: Minimax and Non-Saturating

medium
Unit 20 · Inference0/2 to milestone

For discriminator logits on real data d_real and on generated data d_fake:

  • discriminator loss: −E[log⁡σ(dreal)]−E[log⁡(1−σ(dfake))]-\mathbb E[\log\sigma(d_{real})] - \mathbb E[\log(1 - \sigma(d_{fake}))]
  • minimax generator loss: E[log⁡(1−σ(dfake))]\mathbb E[\log(1 - \sigma(d_{fake}))]
  • non-saturating generator loss: −E[log⁡σ(dfake)]-\mathbb E[\log\sigma(d_{fake})]

Implement d_loss(d_real, d_fake), g_loss_minimax(d_fake) and g_loss_nonsat(d_fake) stably from logits (use log⁡σ(z)=−softplus(−z)\log\sigma(z) = -\text{softplus}(-z)), plus g_grads(d_fake) returning the gradient of each generator loss with respect to the logits as a tuple (minimax_grad, nonsat_grad) (mean over the batch).

#gans#losses#generative-models2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
GAN Losses: Minimax and Non-Saturating · TechGradify