ML11 min read

Generative Adversarial Networks (GANs)

Understand how GANs work by pitting two networks against each other to generate realistic data.

Sarah Chen
December 19, 2025
0.0k0

Generative Adversarial Networks (GANs)

GANs generate data so realistic that humans can't tell it's fake. They do this through an adversarial game between two networks.

The Game

Generator (G): Creates fake data from random noise
Discriminator (D): Tries to distinguish real from fake

They compete:

  • G tries to fool D
  • D tries to catch G

As they improve, G generates increasingly realistic data.

Random Noise ──> [Generator] ──> Fake Image
                                    │
                              [Discriminator] ──> Real or Fake?
                                    │
Real Image ─────────────────────────┘

Simple GAN Implementation

import tensorflow as tf
from tensorflow.keras.layers import Dense, LeakyReLU, BatchNormalization, Reshape, Flatten
from tensorflow.keras.models import Sequential

# Generator: noise -> image
def build_generator(latent_dim):
    model = Sequential([
        Dense(256, input_dim=latent_dim),
        LeakyReLU(0.2),
        BatchNormalization(),
        Dense(512),
        LeakyReLU(0.2),
        BatchNormalization(),
        Dense(1024),
        LeakyReLU(0.2),
        BatchNormalization(),
        Dense(784, activation='tanh'),
        Reshape((28, 28, 1))
    ])
    return model

# Discriminator: image -> real/fake
def build_discriminator():
    model = Sequential([
        Flatten(input_shape=(28, 28, 1)),
        Dense(512),
        LeakyReLU(0.2),
        Dense(256),
        LeakyReLU(0.2),
        Dense(1, activation='sigmoid')
    ])
    return model

latent_dim = 100
generator = build_generator(latent_dim)
discriminator = build_discriminator()

Training Loop

cross_entropy = tf.keras.losses.BinaryCrossentropy()
g_optimizer = tf.keras.optimizers.Adam(1e-4)
d_optimizer = tf.keras.optimizers.Adam(1e-4)

@tf.function
def train_step(real_images):
    batch_size = tf.shape(real_images)[0]
    noise = tf.random.normal([batch_size, latent_dim])
    
    with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
        # Generate fake images
        fake_images = generator(noise, training=True)
        
        # Discriminator predictions
        real_output = discriminator(real_images, training=True)
        fake_output = discriminator(fake_images, training=True)
        
        # Losses
        d_loss_real = cross_entropy(tf.ones_like(real_output), real_output)
        d_loss_fake = cross_entropy(tf.zeros_like(fake_output), fake_output)
        d_loss = d_loss_real + d_loss_fake
        
        g_loss = cross_entropy(tf.ones_like(fake_output), fake_output)
    
    # Update weights
    d_gradients = disc_tape.gradient(d_loss, discriminator.trainable_variables)
    g_gradients = gen_tape.gradient(g_loss, generator.trainable_variables)
    
    d_optimizer.apply_gradients(zip(d_gradients, discriminator.trainable_variables))
    g_optimizer.apply_gradients(zip(g_gradients, generator.trainable_variables))
    
    return g_loss, d_loss

Common GAN Variants

Type Use Case
DCGAN Images using convolutions
StyleGAN High-quality face generation
CycleGAN Image-to-image translation
Pix2Pix Paired image translation
WGAN More stable training

Training Challenges

GANs are notoriously hard to train:

  1. Mode Collapse: Generator produces limited variety
  2. Training Instability: D or G dominates
  3. Vanishing Gradients: G can't learn when D is too good

Practical Tips

# Tips for stable training:

# 1. Use labels wisely (label smoothing)
real_labels = 0.9  # Not 1.0
fake_labels = 0.0

# 2. Add noise to discriminator inputs
noisy_real = real_images + 0.1 * tf.random.normal(tf.shape(real_images))

# 3. Train D more often than G
for _ in range(2):  # Train D twice
    train_discriminator(real_images)
train_generator()

# 4. Use spectral normalization
from tensorflow_addons.layers import SpectralNormalization
Dense = SpectralNormalization(Dense(256))

Applications

  • Face generation and editing
  • Image super-resolution
  • Data augmentation
  • Art creation
  • Video generation
  • Drug discovery

Key Takeaway

GANs learn to generate data through adversarial training between generator and discriminator. They produce amazingly realistic images but are tricky to train. Start with DCGAN for images, use established architectures, and monitor both losses carefully. For production use, consider pre-trained StyleGAN models.

#Machine Learning#Deep Learning#GAN#Generative Models#Advanced