⚔️ The Minimax Duel: How Adversarial Competition Drives Unprecedented Generative Fidelity
Introduced by Ian Goodfellow et al. in 2014, Generative Adversarial Networks (GANs) revolutionized unsupervised machine learning by framing generation as a zero-sum game between two competing neural networks. The Generator ($G$) acts as a counterfeiter, taking random noise vectors and attempting to synthesize convincing fake data samples, while the Discriminator ($D$) acts as a police officer, evaluating data to distinguish between real training examples and fake synthetic outputs. Through continuous backpropagation and simultaneous gradient updates, both networks engage in an evolutionary arms race until the Generator produces outputs indistinguishable from empirical reality.
import torch
import torch.nn as nn
class Generator(nn.Module):
def __init__(self, latent_dim=100, channels=3):
super().__init__()
self.net = nn.Sequential(
nn.ConvTranspose2d(latent_dim, 512, 4, 1, 0, bias=False),
nn.BatchNorm2d(512),
nn.ReLU(True),
nn.ConvTranspose2d(512, 256, 4, 2, 1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(True),
nn.ConvTranspose2d(256, channels, 4, 2, 1, bias=False),
nn.Tanh() # Output range [-1, 1]
)
def forward(self, z):
return self.net(z)
class Discriminator(nn.Module):
def __init__(self, channels=3):
super().__init__()
self.net = nn.Sequential(
nn.Conv2d(channels, 64, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64, 128, 4, 2, 1, bias=False),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(128, 1, 4, 1, 0, bias=False),
nn.Sigmoid()
)
def forward(self, img):
return self.net(img).view(-1, 1)
# SRGAN Loss = Adversarial Loss (Discriminator fooling) + VGG Perceptual Loss (Feature map MSE)
# Ensures upscaled images maintain sharp building edges and foliage textures rather than blurry bicubic smoothing.
# Cycle-Consistency Loss: X -> G(X) -> F(G(X)) ≈ X
# Ensures that translating a summer image to winter and back yields the original summer photograph.
def compute_gradient_penalty(discriminator, real_samples, fake_samples, device):
alpha = torch.rand(real_samples.size(0), 1, 1, 1, device=device)
interpolates = (alpha * real_samples + ((1 - alpha) * fake_samples)).requires_grad_(True)
d_interpolates = discriminator(interpolates)
gradients = torch.autograd.grad(
outputs=d_interpolates, inputs=interpolates,
grad_outputs=torch.ones_like(d_interpolates),
create_graph=True, retain_graph=True, only_inputs=True
[0]
gradients = gradients.view(gradients.size(0), -1)
gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean()
return gradient_penalty
# Combines unsupervised embedding network loss, supervised autoregressive loss, and adversarial generator loss.
# Produces synthetic time-series data streams indistinguishable from real bank customer ledgers.
# Step 1: Human parsing network extracts body silhouette.
# Step 2: Geometric matching module aligns clothing item to pose keypoints.
# Step 3: Inpainting GAN blends garment edges realistically onto customer torso.
# Feeds clear weather frames + conditioning vector (e.g. "fog_density: 0.8") into Generator
# to synthesize extreme driving scenarios safely inside software simulation loops.
# Evaluates audio waveform realism across multiple receptive field resolutions using multi-scale discriminators.
# Injects optimized adversarial noise δ into images such that ||δ||_p <= ε while causing target classifier misclassification.
import scipy.linalg
import numpy as np
def calculate_fid(mu1, sigma1, mu2, sigma2):
diff = mu1 - mu2
covmean, _ = scipy.linalg.sqrtm(sigma1.dot(sigma2), disp=False)
if np.iscomplexobj(covmean):
covmean = covmean.real
fid = diff.dot(diff) + np.trace(sigma1 + sigma2 - 2 * covmean)
return fid