The Reserve Bank of India is locked in a contest it can never fully win. Every banknote it issues carries security features: a watermark of Mahatma Gandhi visible when held up to light, a security thread woven through the paper, ink on the numeral that shifts color when the note is tilted, raised printing that a visually impaired person can identify by touch. Counterfeiters study these features and try to copy them. When fakes get convincing enough to pass a bank teller's glance, the RBI adds a new feature. When that feature gets copied too, it adds another. Neither side ever finishes the job; each one only gets sharper because the other is trying just as hard.
In 2014, a doctoral student at the University of Montreal named Ian Goodfellow noticed that this exact contest (one side forging, the other side detecting, each improving only because of the other) could be turned into a way to train neural networks. Instead of a human counterfeiter and a human bank teller, he set up two neural networks and made them compete against each other, millions of times, adjusting their internal numbers a little after every round. Eventually the "counterfeiter" network became good enough that its forgeries (images, in this case, not currency) were almost impossible for the "teller" network to catch. Goodfellow called this pair of competing networks a Generative Adversarial Network, or GAN. It is the idea behind websites such as thispersondoesnotexist.com, built on an NVIDIA architecture called StyleGAN, which shows a photorealistic human face every time the page loads. Refresh it a dozen times and you will not see the same face twice, because none of them exist. Every one is invented, pixel by pixel, by a network that was never told what a face is, only shown examples and told to fool its opponent.
Two Networks, One Contest
A GAN is made of two separate neural networks, trained together, that want opposite things.
The first is the generator, usually written G. Its job is to create fake data (in most examples, images) starting from nothing but random numbers. Feed it a vector of, say, 100 random values drawn from a normal distribution (this input is called noise, written z, and the space of all possible z vectors is the latent space), and G transforms that noise through several layers into something shaped like real data: a 64×64 grid of pixel values, for instance. Early in training, since G's weights start out random, this output looks like static. The generator has no idea what a face, a digit, or a saree pattern looks like; it only ever receives random noise as input and a single number as feedback.
The second network is the discriminator, written D. Its job is simpler to state: look at an image and output one number between 0 and 1, representing how confident it is that the image is real rather than something G invented. A well-trained discriminator is a binary classifier, no different in principle from a spam filter or a fraud-detection model. The difference is that instead of a fixed, pre-labelled dataset, half of its "fake" examples are produced on the fly by an opponent that is simultaneously trying to get better at fooling it.
G never sees a single real image directly. The only real data it ever influences are the images D compares its fakes against, and the only signal it receives back is whether D was fooled. This constraint is what makes GAN training interesting: the generator learns to produce realistic data purely by trying to defeat a critic, the way a forger might get better at replicating a watermark purely through rejection at the bank counter, without ever being handed a textbook description of what a watermark looks like.
The Training Loop
Training a GAN means alternating between two updates, repeated for many thousands of rounds:
- Sample a batch of real examples from the training dataset and a batch of random noise vectors
z. Pass the noise throughGto produce a batch of fake examples. - Update
Donly: show it both batches, real examples labelled 1 and fake examples labelled 0, and adjustD's weights to reduce its classification error, exactly like training any binary classifier. - Sample a fresh batch of noise and generate a new batch of fakes with
G. This time, updateGonly: run the fakes throughDagain, but instead of matchingD's judgment, adjustG's weights in the direction that would have pushedD's output closer to 1 ("real") for those same fakes. - Repeat. Each pass nudges
Dinto a slightly sharper judge andGinto a slightly better forger, in response to that sharper judge.
Formally, the two networks are playing a minimax game. D tries to maximize a score that rewards it for correctly labelling real data as real and fake data as fake; G tries to minimize that same score by making its fakes indistinguishable from real data:
min(G) max(D) V(D, G) = E[log D(x)] + E[log(1 - D(G(z)))]
x = a real training example
z = a random noise vector fed into the generator
G(z) = the fake example the generator produces from that noise
D(x) = the discriminator's estimated probability that x is real
D(G(z)) = the discriminator's estimated probability that the fake is real
The first term rewards D for scoring real examples close to 1; the second rewards D for scoring fakes close to 0 (since a low D(G(z)) makes log(1 - D(G(z))) large). G has no influence over the first term, since it never appears there, so G's entire incentive is to push D(G(z)) as close to 1 as it can.
Tracing the Numbers: A Worked Example
In practice, most implementations replace G's half of that formula with a slightly different loss. Minimizing log(1 - D(G(z))) gives a weak gradient early in training: when D easily rejects G's first, clumsy attempts, D(G(z)) sits near 0, and the slope of log(1 - D(G(z))) in that region is too shallow to push G forward quickly. So instead, most training code has G minimize -log(D(G(z))), whose slope near D(G(z)) = 0 is much steeper, exactly where the extra push is needed. Goodfellow's original paper recommends this substitution; it changes nothing about what the two networks are trying to achieve, only how sharply the training signal responds while G is still bad.
With that substitution, both networks' losses are ordinary binary cross-entropy, the same loss function used to train any yes/no classifier. The following function computes both losses directly from D's confidence scores:
import math
def d_loss(d_real, d_fake):
return -0.5 * (math.log(d_real) + math.log(1 - d_fake))
def g_loss(d_fake):
return -math.log(d_fake)
print(round(d_loss(0.9, 0.1), 3), round(g_loss(0.1), 3)) # Round 1
print(round(d_loss(0.8, 0.4), 3), round(g_loss(0.4), 3)) # Round 2
print(round(d_loss(0.5, 0.5), 3), round(g_loss(0.5), 3)) # Round 3
Running this prints 0.105 2.303, then 0.367 0.916, then 0.693 0.693. Each line traces one stage of training:
- Round 1:
D(real) = 0.9,D(fake) = 0.1.D's loss is low (0.105): it is barely being fooled, so it has little reason to change.G's loss is high (2.303): almost all of its fakes are caught, so the gradient pushing it to improve is large. - Round 2: after several updates,
Ghas improved enough thatD(fake)rises to 0.4, andD, now facing harder fakes, slips toD(real) = 0.8.D's loss climbs to 0.367 (its job has gotten harder) whileG's loss falls to 0.916 (it is succeeding more often). - Round 3:
G's fakes have become statistically indistinguishable from real data, soDcan do no better than a coin flip on either one:D(real) = D(fake) = 0.5. Both losses converge to the same value, 0.693, which isln(2). Goodfellow's paper proves why: at the game's true equilibrium, the optimal discriminator outputs exactly 0.5 for every input, real or fake, because the two distributions have become identical, which is precisely the condition reached in this round.
That convergence point is the real target of GAN training: a discriminator that is mathematically unable to do better than guessing, because the generator's output has become inseparable from the real data it was trained against.
Building a Tiny GAN
The logic above is exactly what real training code does, wrapped in a deep learning framework so gradients are computed automatically instead of by hand. Below is a complete, minimal GAN in PyTorch that learns to generate numbers clustered around 4. It is a toy problem small enough to run on a laptop in seconds, but it is built from the same two networks and the same alternating updates used to train GANs that generate megapixel images.
import torch
import torch.nn as nn
class Generator(nn.Module):
def __init__(self):
super().__init__()
self.net = nn.Sequential(
nn.Linear(1, 16),
nn.ReLU(),
nn.Linear(16, 1)
)
def forward(self, z):
return self.net(z)
class Discriminator(nn.Module):
def __init__(self):
super().__init__()
self.net = nn.Sequential(
nn.Linear(1, 16),
nn.ReLU(),
nn.Linear(16, 1),
nn.Sigmoid()
)
def forward(self, x):
return self.net(x)
G, D = Generator(), Discriminator()
loss_fn = nn.BCELoss()
opt_G = torch.optim.Adam(G.parameters(), lr=0.001)
opt_D = torch.optim.Adam(D.parameters(), lr=0.001)
for epoch in range(5000):
real = 4 + 0.5 * torch.randn(64, 1)
real_labels, fake_labels = torch.ones(64, 1), torch.zeros(64, 1)
# Train the discriminator on one batch of real, one of fake
fake = G(torch.randn(64, 1)).detach()
d_loss = loss_fn(D(real), real_labels) + loss_fn(D(fake), fake_labels)
opt_D.zero_grad()
d_loss.backward()
opt_D.step()
# Train the generator to make D call its output "real"
fake = G(torch.randn(64, 1))
g_loss = loss_fn(D(fake), real_labels)
opt_G.zero_grad()
g_loss.backward()
opt_G.step()
print(G(torch.randn(5, 1))) # five freshly generated numbers
Notice how directly this mirrors the worked example above. d_loss is the same two-term binary cross-entropy computed by hand in d_loss() a moment ago, now computed on batches of 64 numbers instead of single confidence scores. .detach() stops gradients from flowing into G while D is being trained, since only D should learn at that step. On the generator's turn, fake is not detached, so calling g_loss.backward() sends gradients back through D and into G's weights, even though only opt_G.step() actually applies them. Check G's output every few hundred epochs while this runs: at the start, with G's weights randomly initialized, its outputs sit close to zero with no relationship to the real data. As training proceeds, they drift toward the real cluster and tighten around 4, the mean of the "real" data G never once saw directly, only inferred through thousands of rounds of D's feedback.
When the Contest Breaks: Mode Collapse and Instability
Ordinary supervised learning trains one network against a fixed target, and gradient descent reliably pulls its loss downward. A GAN trains two networks against a moving target (each other), and nothing guarantees the contest stays fair. Two failure patterns show up often enough that every GAN practitioner learns to recognize them.
The first is mode collapse. Real datasets are diverse: millions of different faces, digits written in thousands of distinct handwriting styles. G's only goal, though, is to fool D, and if it stumbles onto a handful of outputs that reliably do that, it has no incentive to explore anything else. A GAN meant to generate all ten digits, trained badly, might collapse into producing only convincing 3s and 8s, ignoring the rest, because that narrow slice was enough to fool the current discriminator. D may eventually catch on and start rejecting those same digits, at which point G often collapses onto a different narrow slice rather than learning the full diversity of the real data, like a counterfeiter who perfects a single denomination and never bothers with the rest.
The second is training instability. Because D and G are updated in alternation rather than jointly, the pair can oscillate instead of converging: D sharpens, G overcorrects, D sharpens against the overcorrection, and so on, without settling near the equilibrium traced out above. If D becomes too good too early (confidently rejecting every one of G's attempts), the earlier point about gradient strength stops being theoretical: G receives almost no useful signal about which direction to move in, and training stalls. Later variants such as the Wasserstein GAN, which replaces the discriminator's classification score with a distance-like measure between the real and fake distributions, were built specifically to make this training process more stable, though no fix has made GAN training as reliably convergent as ordinary supervised learning.
What GANs Have Actually Built
Since Goodfellow's 2014 paper, the same generator-versus-discriminator idea has been adapted into a wide range of specialized systems:
- StyleGAN (NVIDIA, 2018-2019) generates photorealistic human faces with fine control over style at different levels of detail. It is the architecture behind thispersondoesnotexist.com and behind several synthetic stock-photo and virtual-model services.
- pix2pix (2017) translates one type of image into another when paired training examples exist, such as turning building sketches into photographs or daytime street photos into their nighttime equivalents.
- CycleGAN (2017) performs a similar translation, turning photographs into Monet-style paintings or horses into zebras, without needing paired examples, by adding a constraint that translating an image and then translating it back should reconstruct the original.
- SRGAN (2017) upscales low-resolution images into sharper, higher-resolution versions, filling in plausible detail instead of simply stretching pixels.
- Synthetic data generation for domains where real examples are scarce or sensitive: researchers have used GANs to generate additional training images for medical-imaging models, and synthetic transaction-like data to help train fraud-detection systems where genuine fraud cases are rare compared to legitimate ones.
GANs dominated image-generation research for roughly the seven years after Goodfellow's paper. Since around 2022, a different family of models called diffusion models has taken over as the leading approach behind tools such as Stable Diffusion and DALL-E; these build an image by starting from pure noise and removing it gradually across many small steps, rather than producing an image in one forward pass. GANs have not disappeared: generating an image in a single pass makes them faster at inference time, so they remain the practical choice for several real-time and specialized tasks. But the core lesson GANs taught the field, that two networks trained against each other can learn things neither could learn alone, outlasted their time at the top of the leaderboard, and shows up today inside adversarial training methods used well beyond image generation.
The Ethics of Synthetic Reality
The same mechanism that lets a GAN generate a face that has never existed can generate a face that looks exactly like someone who does. The term deepfake (a blend of "deep learning" and "fake") entered public use after a Reddit account by that name began posting face-swapped videos in late 2017, and the underlying techniques, many of them GAN-based, have improved sharply since. In November 2023, a fabricated video morphing actor Rashmika Mandanna's face onto someone else's body spread widely across Indian social media before being identified as synthetic, drawing public statements of concern from Bollywood figures and prompting India's IT Ministry to issue fresh advisories directing platforms to identify and act on deepfake content. It was a concrete demonstration, to a national audience, of exactly the capability this chapter has built up mathematically: a generator good enough that the humans looking at its output cannot tell.
None of this makes GANs unsafe to study. The same architecture that can misrepresent a real person also restores damaged historical photographs, generates synthetic training data for medical models where patient privacy makes real data hard to share, and powers computer-graphics tools that millions of people use for entertainment every day. It is a reason the field now spends almost as much effort detecting synthetic media as generating it: classifiers trained specifically to spot GAN artifacts, and industry provenance standards such as C2PA, backed by a coalition of major technology and media companies, that aim to attach a verifiable record of how and when a piece of media was captured or generated. Every engineer who builds a generative model inherits some responsibility for the detection side of that same problem.
Back to the Reserve Bank
The RBI's contest with counterfeiters never produces a final winner, only a currency that keeps getting harder to fake because the two sides keep pushing each other. A well-trained GAN reaches its version of that same non-ending: a generator whose output a discriminator can no longer tell apart from the real thing, at the exact 50-50 point traced numerically earlier in this chapter. The adversarial part of the name describes the entire training mechanism. Remove the contest, and nothing is left pushing the generator to improve at all.
The tiny GAN built in this chapter takes a few thousand rounds to learn a single number, 4. StyleGAN, trained the same way in principle, needed an NVIDIA research team, a large dataset of real photographs, and substantial GPU compute to learn faces instead. The gap between those two is mostly a matter of scale: the same generator, discriminator, and alternating updates traced by hand above are still running underneath. Anyone with a laptop and a free Google Colab notebook can run the training loop from this chapter in under a minute and watch a cluster of random numbers visibly pull itself toward its target, the same contest that, scaled up by several orders of magnitude, can produce a face that has never existed, or convincingly borrow one that has.
Think About It
Think about this: How would you explain gans: ai that creates to a friend who has never seen a computer? What real-world analogy would you use? Imagine you had to build a system using these concepts — what would be your first step? Try this: before moving on, write down three things you learned and one question you still have.
Key Takeaways — Summary and Recap
Let us recap what we covered: the core ideas behind gans: ai that creates, how they connect to real-world applications, and why they matter for your journey in computer science. Remember these key points as you move forward. For competitive exam preparation (CBSE, JEE, BITSAT), focus on understanding the WHY behind each concept, not just the WHAT.