Phase 08: Generative AI

发电机与区分器

2014年,Goodfellow的技巧是完全跳过密度.两个网络.一个制造假冒.一个抓住他们.他们战斗直到假冒是无法区分的真实.它不应该工作.它经常不会.当它做的时候,样本仍然是最尖的文学中,对于狭窄域.

Type: Build

Languages: Python

Prerequisites: Phase 3 · 02 (Backprop), Phase 3 · 08 (Optimizers), Phase 8 · 02 (VAE)

Time: ~75 minutes

问题

由于它们的MSE解码器损失是平均图像的最佳,而许多可信数字的平均值是模糊的数字.你想要一个损失,以奖励可信性,而不是像素智能接近任何一个目标.可信性没有封闭形式.你必须学习它.

善良的想法:训练一个分类器D(x)让一个发电机训练一个发电机.G(z)愚蠢D输出信号G是什么都不一样D现在的信号是:G如果两个网络融合,G没有写下数据的分布.log p(x)现在,我们要去.

这是一场对抗训练.数学是一场最小级游戏:

\min_G \max_D \; \mathbb{E}_{\text{real}}[\log D(x)] + \mathbb{E}_{\text{fake}}[\log(1 - D(G(z)))]

2026年,GAN不再是SOTA生成器 (扩散和流量匹配吞了那冠).但StyleGAN 2/3仍然是有史以来出货的最尖的面型,GAN歧视器被用于扩散训练中的感知损失,而对抗训练支持快速的1步蒸 (SDXL-Turbo,SD3-Turbo,LCM) 允许您出货实时扩散.

概念

!GAN training: generator and discriminator in minimax

Generator G(z).绘制一个噪音向量z \sim \mathcal{N}(0, I)给一个样本\hat{x}电脑系统的电脑系统.

Discriminator D(x).绘制一个样本的échar概率 (或分数).真 → 1,假 → 0.

Loss.两次交替更新:

  • Train D: \text{loss}_D = -[ \log D(x) + \log(1 - D(G(z))) ]双向交叉值在真=1,假=0.
  • Train G: \text{loss}_G = -\log D(G(z))这是Goodfellow使用的 不和的形式 (原始\log(1 - D(G(z)))化和杀死梯度时D对于其他国家,

Training loop.一步走D现在,我们要做一个好事.G复制.

Why it works.如果G非常合适p_{\text{data}}现在D没有机会, 没有机会, 没有机会.G没有任何梯度,平衡.

Why it breaks.模式崩 (G找到一个模式D它们的度变化,D学习得太快,\log D培训不稳定性 (学习率,批量,任何东西).

让GAN工作的变体

YearInnovationFix
2015DCGANConv/deconv, batch norm, LeakyReLU — the first stable architecture.
2017WGAN, WGAN-GPReplace BCE with Wasserstein distance + gradient penalty. Fixes vanishing gradient.
2017Spectral normalizationLipschitz-bound the discriminator. Still used in 2026 discriminators.
2018Progressive GANTrain low-res first, add layers. First megapixel results.
2019StyleGAN / StyleGAN2Mapping network + adaptive instance norm. State of the art for fixed-domain photorealism.
2021StyleGAN3Alias-free, translation-equivariant — still the face gold standard in 2026.
2022StyleGAN-XLConditional, class-aware, larger scale.
2024R3GANRebrands with stronger regularization; works on 1024² without tricks.

建立它

code/main.py导电和分辨器是单层隐藏MLP.我们手动执行前进,后退和最小x循环.目标是看到两个关键故障模式 (模式崩 +消失梯度) 发生.

步骤1:不和损失

瓦尼莉的好友失去了\log(1 - D(G(z)))在这个时候,G的梯度基本上是零 G不能改善.非和形式-\log D(G(z))它们在D自信时爆炸,给G一个强烈的信号.

python
def g_loss(d_fake):
    # maximize log D(G(z))  <=>  minimize -log D(G(z))
    return -sum(math.log(max(p, 1e-8)) for p in d_fake) / len(d_fake)

步骤2:每一个生成器步骤的一个歧视步骤

python
for step in range(steps):
    # train D
    real_batch = sample_real(batch_size)
    fake_batch = [G(z) for z in sample_noise(batch_size)]
    update_D(real_batch, fake_batch)

    # train G
    fake_batch = [G(z) for z in sample_noise(batch_size)]  # fresh fakes
    update_G(fake_batch)

对于G来说,新鲜的假冒,否则梯度是陈旧的.

步骤3: 警模式崩

python
if step % 200 == 0:
    samples = [G(z) for z in sample_noise(500)]
    mode_a = sum(1 for s in samples if s < 0)
    mode_b = 500 - mode_a
    if min(mode_a, mode_b) < 50:
        print("  [!] mode collapse: one mode is starved")

法症状:两个真正的模式中的一个停止生成. 歧视者停止纠正它,因为它从来没有被视为假的.

陷

  • Discriminator too strong.如果D达到95%以上的准确度,G就死了.
  • Generator memorizes a mode.加入噪音到D输入,使用微批量区分器层,或切换到WGAN-GP.
  • Batch norm leaking statistics.实际批量+假批量通过同一BN层流动混合他们的统计数据.
  • Inception-score gaming.在低样本数量时,FID和IS有噪音.在 eval时使用≥10k样本.
  • One-shot sampling is a lie for conditional tasks.你仍然需要CFG尺度,切割技巧,再采样才能得到可用的输出.

用它

根据"2026年"的GAN堆:

SituationPick
Photoreal human faces, fixed poseStyleGAN3 (sharpest, smallest)
Anime / stylized facesStyleGAN-XL or Stable Diffusion LoRA
Image-to-image translationPix2Pix / CycleGAN (Phase 8 · 04) or ControlNet (Phase 8 · 08)
Fast 1-step text-to-imageAdversarial distillation of diffusion (SDXL-Turbo, SD3-Turbo)
Perceptual loss inside a diffusion trainerSmall GAN discriminator on image crops
Anything multi-modal, open-endedDon't — use diffusion or flow matching

网页的数据源是很简单的,但很窄的.一旦您的域名打开了照片,任意的文本提示,视频转向扩散.

运送它

保存outputs/skill-gan-debugger.md技能采用一个失败的GAN运行 (损失曲线,样本格格,数据集大小) 并输出了排列可能原因列表,一线修复和重复运行协议.

运动

  1. Easy.跑步code/main.py根据股票设置.D_LR = 5 * G_LR几快G的损失会崩到恒定?
  2. Medium.取代Goodfellow BCE损失的WGAN损失: \text{loss}_D = \mathbb{E}[D(\text{fake})] - \mathbb{E}[D(\text{real})]现在\text{loss}_G = -\mathbb{E}[D(\text{fake})]子 D 的重量到[-0.01, 0.01]训练是否更稳定?
  3. Hard.扩展1D示例到2D数据 (环上混合8个高西安).追踪发电机在1k,5k,10k步骤中捕获了8种模式中的多少种.实施微批次差异和重新测量.

关键词

TermWhat people sayWhat it actually means
Generator"G"Noise-to-sample network, G: z → x̂.
Discriminator"D"Classifier D: x → [0, 1], real vs fake.
Minimax"The game"\min_G \max_D of a joint objective.
Non-saturating loss"The fix"Use -\log D(G(z)) for G instead of \log(1 - D(G(z))).
Mode collapse"G memorized one thing"Generator produces few distinct outputs despite diverse data.
WGAN"Wasserstein"Replace BCE with Earth-Mover distance + gradient penalty; smoother gradient.
Spectral norm"Lipschitz trick"Constrain D's weight norms to bound its slope; stabilizes training.
StyleGAN"The one that works"Mapping network + AdaIN; best-in-class for faces, still in 2026.

产品说明:一次性推断是GAN的持久优势

在生产-推理文献词汇中,一个GAN有:

  • No prefill, no decode stages.一个单身的G(z)预测时间:
  • No KV-cache pressure.只有重量,批量量由激活内存限制,而不是缓存.
  • Trivial continuous batching.由于每个请求都采用相同的固定FLOP,因此在服务器的目标占用量上静态批量通常是最佳的.

这就是为什么GAN蒸 (SDXL-Turbo,SD3-Turbo,ADD,LCM) 是2026年快速文字到图像的主导技术:它将20-50步的扩散管道分解成1-4步GAN式前进通道,同时保持了扩散基的分布.对抗损失作为将慢发电器转化为快速发电器的训练时间.

进一步阅读

This free lesson is part of the AI Engineering from Scratch curriculum. Read the full explanation, run the lesson code, and verify the result in the interactive reader or from the repository source.

Browse the complete course catalog or open this lesson on GitHub.