GANs (Generative Models)
A forger and a detective training each other to madness. That's the short version of GANs.
Try it
Interactive GAN Training
"Real" Images Reference images that teach the Discriminator what "real" looks like.
Generator The Generator creates new images from random noise, trying to fool the Discriminator.
Judged The current image being evaluated by the Discriminator is shown here.
Discriminator The Discriminator evaluates images, trying to distinguish real from generated ones.
Success Rates Over Time
What does this demo show?
Two networks in a contest: a forger paints images, a checker decides real or fake. Out of the contest come strikingly real new images.
- What you see
- On the left a stack of real example images. Beside it the generator with its current image and a small grid of its recent attempts. In the middle the image being judged, on the right the checker with a bar running from "fake" to "real". Arrows show how an image travels through the stations.
- What happens
- At the start the forger's image is coarse noise, blurry and patchy. Round after round it grows sharper and looks more like the real examples. The grid fills with ever more convincing fakes as forger and checker take turns learning from each other.
- What you can do
- Start or pause the training, step through image by image, or reset everything. The speed slider sets the pace of the contest. The style settings decide what the real images look like: shapes, count, size and colours.
- What to watch for
- Watch the fake image climb from noise toward something that looks real, drawing ever closer to the stack of real images. This tug-of-war between forger and checker is exactly what produces new, strikingly real images. If every grid cell suddenly looks the same, the forger has lost its variety.
Definition
What are GANs?
Art Forger Analogy
Imagine two artists working together in a unique way:
The Generator is like a forger trying to create fake paintings that look like masterpieces.
The Discriminator is like an art expert trying to spot the fakes from the real ones.
As they compete, both get better - the forger creates more realistic fakes, and the expert becomes better at spotting them. This competition drives both to improve!
Scientific Explanation
GANs consist of two neural networks competing in an adversarial game:
Generator Network G(z): Transforms random noise z into synthetic data samples that mimic the training distribution.
Discriminator Network D(x): Binary classifier that estimates the probability that a sample came from the training data rather than G.
The networks are trained simultaneously through adversarial training: min_G max_D V(D,G) = E[log D(x)] + E[log(1-D(G(z)))]
How GANs Work
- Initialization: Both networks start with random weights. The generator produces random noise, and the discriminator makes random guesses.
- Generator Creates: The generator takes random noise as input and tries to transform it into realistic-looking data (in our case, patterns of shapes).
- Discriminator Judges: The discriminator sees both real data and generated data, trying to correctly classify each as 'real' or 'fake'.
- Both Learn: Based on the discriminator's judgment, both networks update their weights - the generator to fool the discriminator better, and the discriminator to spot fakes better.
Mode Collapse: Mode collapse occurs when the generator produces a limited number of outputs that fool the discriminator. Cause: The generator finds local minima in the loss function and loses diversity. Recognizable by: All outputs look similar (e.g., only yellow triangles). Solutions: Minibatch discrimination, feature matching, or unrolled GANs.
The Adversarial Principle
GANs consist of two neural networks competing against each other: The Generator creates fake data (e.g. images), while the Discriminator tries to distinguish real from fake data. Through this competition, both networks continuously improve.
The Generator learns to produce increasingly realistic outputs to fool the Discriminator. The Discriminator simultaneously learns to better distinguish between real and fake. This adversarial process leads to amazingly realistic generated data.
Advantages of GANs
- High-quality generation: Produces very realistic images, videos and other data
- Unsupervised learning: Requires no labeled training data
- Versatile: Applicable to images, audio, text and more
- Creativity: Can generate novel, never-seen variants
Challenges
GANs are notoriously difficult to train: Mode collapse (Generator produces only few variants), unstable training, and difficult hyperparameter selection are common problems. Training requires significant computational power and expertise.
Practical Applications
Image generation and editing, style transfer, data augmentation for ML training, drug design, speech synthesis, and creative applications in art and design.
Try the demo! Watch how Generator and Discriminator improve each other.
1
# GANs Training Algorithm
2
function train_GANs(epochs, learning_rate):
3
# Main function: takes training epochs and learning rate
4
generator = create_generator_network()
5
discriminator = create_discriminator_network()
6
7
for each epoch in epochs:
8
# An epoch = one complete training pass
9
10
# Step 1: Train the Discriminator
11
real_image = get_real_image_from_dataset()
12
real_label = 1 # "This is real"
13
14
# Random numbers are the input for the generator
15
noise = generate_random_numbers(100)
16
fake_image = generator.generate(noise)
17
fake_label = 0 # "This is fake"
18
19
# Calculate Discriminator Loss
20
# Should recognize real images as "real" (value close to 1)
21
d_loss_real = -log(discriminator(real_image))
22
# Should recognize fake images as "fake" (value close to 0)
23
d_loss_fake = -log(1 - discriminator(fake_image))
24
d_loss_total = d_loss_real + d_loss_fake
25
26
discriminator.update_weights(d_loss_total, learning_rate)
27
28
# Step 2: Train the Generator
29
new_noise = generate_random_numbers(100)
30
generated_image = generator.generate(new_noise)
31
32
# Generator wants to fool Discriminator
33
# Goal: images should be classified as "real"
34
g_loss = -log(discriminator(generated_image))
35
36
generator.update_weights(g_loss, learning_rate)
37
38
if epoch % 100 == 0:
39
show_progress(epoch, d_loss_total, g_loss)
40
save_sample_images(generator)
🎨 Initialization: The Artist and the Critic
Generator (artist) and Discriminator (critic) are created as neural networks. Both start with random weights - like an artist without experience and a critic without art knowledge.
generator = create_generator_network()
discriminator = create_discriminator_network()
📊 Prepare Dataset
Thousands of real images are loaded as training foundation. These define what 'real' means - the target distribution the generator must learn.
🔍 Discriminator: The Critic
Learns like an art expert: Analyzes real art, studies forgeries, develops an eye for details and authenticity. Its goal: Perfect discrimination.
🎨 Generator: The Artist
Starts with pure chaos (random noise) and learns to create realistic images from it. Each feedback from the discriminator helps it improve.
⚔️ Adversarial Training
A continuous competition: The generator tries to deceive, the discriminator to detect. This competition drives both to ever better performance.
🏆 Local Optimum
Training converges to an equilibrium point: The generator produces convincing images, the discriminator reaches about 50% accuracy. Mode collapse may occur!
Check
Test Your Understanding
What is the main goal of the generator in a GAN?
Select one answer
1. What is the main goal of the generator in a GAN?
2. What happens during mode collapse?
3. Why do GANs use adversarial training?
4. You observe in the demo that the generator suddenly only produces yellow triangles. What happened?
5. Why is it called 'adversarial'?
6. What would you NOT use GANs for?