The Adversarial Loss
Min-max training in practice.
The Adversarial Loss is a free Deep Learning Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Deep Learning Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Loss Drives Both Players
Every GAN player learns from a loss that scores how well it did. The two losses pull in opposite directions, which is what makes training adversarial.
The Min-Max Game
GAN training is a min-max game: the discriminator tries to maximize its score while the generator tries to minimize it. Neither fully wins.
Binary Cross-Entropy
Most GANs measure each prediction with binary cross-entropy, the standard loss for a real-versus-fake yes or no decision.
criterion = nn.BCELoss()Discriminator on Real Data
First the discriminator sees real images and should call them real. Its real loss pushes those outputs toward the label 1.
out_real = D(real)
loss_real = criterion(out_real, ones)Discriminator on Fakes
Next it judges generated images and should call them fake. The fake loss pushes those outputs toward the label 0.
out_fake = D(fake.detach())
loss_fake = criterion(out_fake, zeros)Why detach the Fakes
When updating the discriminator you call detach on the fakes so gradients do not flow back into the generator on this step.
fake.detach()The Generator's Loss
The generator wants its fakes labeled real, so its loss compares the discriminator's verdict on fakes against the label 1.
out = D(fake)
loss_g = criterion(out, ones)The Non-Saturating Trick
Telling the generator to maximize real-ness, instead of just minimizing fake-ness, gives stronger early gradients. This is the non-saturating loss.
Two Updates, One Round
Each training round does two updates: first the discriminator on its combined loss, then the generator on its own. Order and timing matter.
A Moving Target
Because both networks change every step, each one chases a moving target. This is why GAN losses bounce around instead of falling smoothly.
Losses Are Not Accuracy
In a GAN a falling loss does not mean better images. You judge quality by looking at samples, not by the loss number alone. 👀
Quick Check
One detail trips up many beginners.
Recap: Adversarial Loss
You saw the min-max game: cross-entropy guides both players, fakes are detached for the judge, and samples, not loss, reveal real progress. 🎯
Frequently asked questions
Is the “The Adversarial Loss” lesson free?
Yes — the full text of “The Adversarial Loss” is free to read here on the web, and the Deep Learning Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Deep Learning Academy course, upgrade to CoddyKit PRO.
What will I learn in “The Adversarial Loss”?
Min-max training in practice. You practise Deep Learning Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Deep Learning Academy?
No prior experience is required. Deep Learning Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The Adversarial Loss” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Deep Learning Academy lesson?
Yes. Every Deep Learning Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.