Lecture - 4 : Latent Space & Guidance

4. Latent Space & Guidance

关于Classifier Guidance 和 Classifier-Free Guidance 很清晰的讲解 https://zhuanlan.zhihu.com/p/660518657

1. 问题设定与表示空间

前三讲都在研究 unconditional generation:

  • Lecture 1 用 DDPM 学习 noise prediction;
  • Lecture 2 用 Score Matching 学习 probability density 的局部方向,并用 SDE 统一连续动力学;
  • Lecture 3 用 Flow Matching 直接学习 velocity field。

无论采用哪一种 generation paradigm,之前都暂时忽略了两个工程问题:

  1. 图像和带噪图像究竟应当在哪个空间中表示?
  2. 怎样让模型按照文本、类别或参考图像等条件生成?

这正是 Lecture 4 的任务。整堂课分成三层:

Representation
    图像:Pixel Space -> VAE Latent Space
    条件:Text/Image -> Embeddings
                         |
                         v
Alignment
    CLIP 将 text 与 image 映射到共享语义空间
                         |
                         v
Guided Generation
    Classifier Guidance -> Classifier-Free Guidance

这三层解决不同问题:

  • Latent space 降低生成计算成本;
  • Condition embedding 把文本或图像转成神经网络能够处理的向量;
  • Guidance 决定采样轨迹要在多大程度上服从条件。

学完后,应当能够回答:

  1. 为什么不直接在 pixel space 中训练高分辨率生成模型?
  2. AutoEncoder 为什么能压缩,却不一定产生适合生成的 latent space?
  3. VAE 的 ELBO 为什么包含 reconstruction 与 KL 两项?
  4. 为什么 VAE 容易产生模糊重建,LPIPS 和 adversarial loss 各解决什么问题?
  5. Latent Diffusion 的 VAE 与 generation model 如何分工?
  6. Transformer 与 ViT 分别怎样表示文本和图像条件?
  7. CLIP 如何把 text embedding 与 image embedding 对齐?
  8. Classifier Guidance 为什么表现为 reverse mean 的平移?
  9. Classifier-Free Guidance 为什么不需要额外 classifier?
  10. Guidance scale 为什么提高条件一致性,却可能损害质量与多样性?

1.1 为什么 Pixel Space 不是理想生成空间?

1.1.1 最直接的图像表示

一张 RGB 图像可以表示为:

formula

如果将它展平,维度为:

formula

对于 formula 图像:

formula

在这个空间中执行 diffusion、score matching 或 flow matching,网络需要反复处理数百万维的状态,训练和推理成本都很高。

1.1.2 Pixel Space 的三个问题

第一,high dimensionality。计算量、显存和生成模型的学习难度都会随空间尺寸增长。

第二,redundant information。相邻像素通常高度相关,大片平滑区域包含许多重复的低级信息。让生成模型逐像素处理这些冗余并不经济。

第三,geometry is not meaningful。在 pixel space 中对图像做一个很小的向量扰动,得到的往往只是没有语义的像素噪声。真实图像集中在高维空间中极薄、极尖锐的区域,两个有效图像之间的普通线性路径也未必经过有效图像。

因此,理想表示空间应满足:

Pixel Space 的问题 理想空间的要求
维度过高 Tractable dimension
像素冗余 Compact representation
邻近关系缺乏意义 Meaningful geometry

1.1.3 Semantic 与 Perceptual 不要混淆

课程在这里区分两个后文反复使用的概念。

  • Semantic similarity:关注物体类别、整体结构、global geometry 和低频信息。两张图即使像素不对齐,只要都表达“一只读书的泰迪熊”,语义仍可能接近。
  • Perceptual similarity:关注局部纹理、边缘、细节和人眼观感。轻微平移不应让相似度突然恶化,但毛发、材质和锐度会显著影响感知质量。

生成模型希望在 latent space 中优先解决 semantic structure,再由 decoder 恢复高频 perceptual details。


1.2 AutoEncoder 能否直接学出理想空间?

1.2.1 用重建任务学习表示

普通 AutoEncoder(AE)由两部分组成:

formula
formula

Encoder 把 pixel-space image 压缩到 latent representation,decoder 再将它恢复为图像。训练目标通常是:

formula

重建只是 proxy task。我们并不真正需要一个“复制输入”的系统,而是希望 bottleneck 迫使网络学会保留图像中的关键信息。

1.2.2 Spatial compression ratio

若输入空间大小为 formula,latent feature map 大小为 formula,可定义 spatial compression ratio:

formula

课程给出的常见数量级约为:

formula

注意,空间边长压缩 formula 倍意味着 spatial token 数约压缩 formula 倍;latent channel 数可能增加,因此总维度压缩比还取决于通道数。

1.2.3 Encoder 与 Decoder 在做什么?

Encoder 使用 convolution、pooling 或其他 downsampling operations,把局部像素组合成更低分辨率的 feature maps。

Decoder 使用 upsampling、transposed convolution 或其他重建模块,将 latent feature maps 恢复为 pixel-space image。

1.2.4 普通 AE 的根本不足

AE 的 loss 只要求每个输入能被重建,并没有约束不同样本的 formula 在 latent space 中怎样排列。因此它可能学到:

  • 分散、断裂的 latent clusters;
  • 大量没有训练样本覆盖的空洞;
  • 对插值和 Gaussian sampling 不友好的几何结构。

所以普通 AE 的检查结果是:

Tractable dimension        yes
Compact representation    yes
😕 Meaningful latent geometry not guaranteed

对生成模型来说,这一点很关键。Diffusion 和 Flow Matching 通常从简单 Gaussian 出发,如果 encoded data distribution 与 Gaussian 相差过大且非常尖锐,transport task 仍然很难。


1.3 怎样用 VAE 约束 Latent Space?

1.3.1 从点映射改成分布映射

Variational AutoEncoder(VAE)不再让 encoder 输出唯一的 latent point,而是输出一个条件分布:

formula

通过 reparameterization trick 采样:

formula

这里把随机性移动到与参数无关的 formula 上,使 gradient 能够穿过采样过程回传到 encoder。

为 latent space 指定简单 prior:

formula

Decoder 定义 likelihood:

formula

课程为便于讨论,假设 decoder variance 为固定常数,网络主要预测重建均值。

1.3.2 为什么 Maximum Likelihood 不可直接计算?

VAE 作为生成模型,其数据 likelihood 为:

formula

对高维 formula 穷举积分不可行。与 Lecture 1 的 DDPM 推导相同,我们引入可采样的 variational posterior formula,再寻找 log-likelihood 的 lower bound。

1.3.3 Maximum Likelihood 到底想解决什么?

理想情况下,VAE 希望通过 maximum likelihood 让模型给真实训练图像更高的概率:

formula

等价地,可以把 negative log-likelihood 当作训练 loss:

formula

这是一种训练监督,但不是“输入图像、预测类别标签”式的监督。它要求模型调整参数,使真实数据在模型定义的概率分布下更容易出现。

难点不是不知道应该优化什么,而是 formula 本身包含一个难以计算的高维积分。VAE 假设先采样 latent,再由 decoder 生成图像:

formula

训练数据只告诉我们最终图像 formula,没有告诉我们究竟是哪个 formula 生成了它。因此,计算一张图像的总概率时,必须把所有可能的 latent explanation 都加起来:

formula

可以把它直观地理解为:

一张观测图像 x
    <- 可能由 latent z_a 生成
    <- 也可能由 latent z_b 生成
    <- 还可能由无数其他 z 生成

p_theta(x) = 所有可能 latent 生成 x 的概率贡献之和

当 formula 是高维连续变量时,无法枚举这些可能性;训练还需要计算 formula,所以不能直接使用这个 marginal likelihood 作为可执行的 loss。

Encoder formula 的作用,就是根据已知图像回答“哪些 latent 最可能解释这张图”。它把原本需要搜索整个 latent space 的问题集中到一片容易采样的区域。ELBO 随后提供一个可计算的 surrogate objective:

理想目标:最大化 log p_theta(x)
        ↓ 其中含有不可计算的高维 latent 积分
引入 q_phi(z | x):近似判断哪些 z 能解释 x
        ↓
优化 ELBO:重建图像,同时规范 latent distribution

因此,ELBO 不是随意添加的另一套 loss,而是为了近似 maximum likelihood 构造出的可训练下界。它的 reconstruction term 提供内容重建监督,KL term 则提供 latent distribution regularization。

1.3.4 VAE 的 ELBO

乘除同一个 formula,并使用 Jensen's inequality:

formula

展开后得到:

formula

右侧就是 VAE 的 ELBO。将最大化 ELBO 改写为最小化 loss:

formula

它包含两个竞争目标:

  • Reconstruction term:让 formula 保留足够信息,使 decoder 能恢复 formula;
  • KL regularization:让每个 formula 不要离 prior 太远,使 aggregate latent space 更连续、更容易采样。

在 diagonal Gaussian 设定下,KL 有闭式形式:

formula

1.3.5 KL 权重为什么不能无限大?

若 formula 太小,latent space 仍可能不规则,encoded distribution 与 Gaussian prior 难以对齐。

若 formula 太大,所有输入的 posterior 都会被强行推向同一个 prior:

formula

此时 formula 几乎不再包含关于 formula 的信息,decoder 倾向于忽略 latent variable。这称为 posterior collapse。

VAE 不是让每个编码结果“严格等于标准 Gaussian”,而是在 reconstruction fidelity 与 latent regularity 之间寻找平衡。


1.4 为什么 VAE 重建容易模糊?

1.4.1 Pixel-wise reconstruction 的平均效应

若固定 Gaussian decoder variance,negative log-likelihood 常退化为 pixel-wise MSE:

formula

当多个细节都可能是合理答案时,MSE 的最优预测是条件均值。不同纹理和边缘被平均后,输出就会显得模糊。

VAE 中这个问题更明显,因为一个输入映射到 latent distribution,而不是唯一 latent code,额外不确定性进一步鼓励 decoder 输出平均化结果。

因此,VAE 虽然获得了 tractable、compact、structured latent space,却未必得到足够 truthful and sharp 的重建。

1.4.2 用 Perceptual Loss 比较 Feature Maps

Pixel loss 对轻微位移十分敏感。人眼认为几乎相同的两张图,只要边缘偏移几个像素,也可能产生很大的 pixel MSE。

Perceptual loss 使用预训练网络的中间 feature maps 比较图像:

formula

其中:

  • formula 是第 formula 层 feature map;
  • formula 是经过人类感知数据校准的通道权重;
  • LPIPS 是典型的 learned perceptual metric。

Feature maps 对小范围平移更稳定,也更关注 edges、shapes 与 texture,因此更贴近 human perception。

但 perceptual weight 太高并非没有代价。课程指出,decoder 的 upsampling structure 与过强 perceptual pressure 可能带来 checkerboard artifacts。

1.4.3 用 Adversarial Loss 提高真实感

再引入 discriminator formula,区分真实图像和 decoder reconstruction:

formula

Decoder 的 adversarial objective 可写为:

formula

如果重建过于模糊,discriminator 容易识别为 fake;decoder 因此被迫生成更锐利、更像真实图像的局部细节。

但 adversarial weight 太高也可能让 decoder 忽略 latent content,只追求“看起来真实”,甚至出现 mode collapse 或内容不忠实。

1.4.4 Refined VAE 的完整权衡

用于现代 latent image generation 的 autoencoder,常综合四类目标:

formula

四项分别负责:

Loss 主要作用 过强时的风险
Reconstruction 保留输入内容 平均化、模糊
KL 结构化 latent space Posterior collapse
Perceptual 保留人眼敏感的形状与纹理 Checkerboard artifacts 等伪影
Adversarial 提高锐度与真实感 Mode collapse、忽略 latent content

这里没有一个权重越大越好。VAE 的工程质量来自多目标之间的平衡。

1.4.5 Flow Matching 与图像模糊的关系

需要区分两种不同的“平均”问题:

  1. VAE decoder 的像素平均:给定 latent 后,如果存在多种合理的高频细节,Gaussian likelihood 或 MSE 倾向于预测条件均值,导致纹理和边缘变模糊;
  2. 生成模型对分布的平均:如果模型直接回归一张图像,多个合理结果可能被平均成一个不存在的中间结果。

Flow Matching 主要处理第二个问题,而不是第一个问题。它在 latent space 中学习从简单分布到数据 latent 分布的运输:

formula

不同的初始噪声可以被运输到不同的 latent mode:

formula

因此,FM 不必像 pixel-wise MSE 那样把多个可能结果直接平均成一张图,而是可以通过采样保留多种合理的 latent 结果。其直觉是:

直接 pixel MSE 回归:多种合理图像 -> 一张平均图像 -> 可能模糊
Flow Matching 生成:一个 latent 分布 -> 多个采样结果 -> 保留多模态

但 FM 最终仍然需要调用 VAE decoder:

formula

如果 VAE decoder 因为压缩或 Gaussian reconstruction 本身已经丢失了高频信息,那么即使 FM 完美地生成了目标 latent,也不能凭空恢复这些信息。因此:

VAE decoder:决定 latent 能否被清晰地解码成图像
Flow Matching:决定 latent 分布和生成模式是否被正确运输

实际系统通常分别处理这两个问题:用 perceptual loss、adversarial loss 和更强的 decoder 减少 VAE 重建模糊;用 Flow Matching 建模 latent 分布,并通过不同初始噪声保持生成多样性。Reflow 主要降低运输路径曲率和采样误差,也不是专门的去模糊方法。


1.5 Latent Diffusion 怎样使用 VAE?

1.5.1 两阶段训练

第一阶段训练 VAE:

formula

第二阶段冻结 VAE,在 encoded latent distribution 上训练 image generation model。对数据图像:

formula

随后在 formula 空间中执行 DDPM、Score Matching 或 Flow Matching,而不再在原始 pixel space 中执行。

例如 latent diffusion 的带噪状态可写为:

formula

训练 noise predictor:

formula

这里 generation model 的参数记为 formula,用来与 VAE encoder/decoder 参数区分。

1.5.2 推理流程

生成时:

  1. 从 latent Gaussian noise 采样;
  2. 在 latent space 中运行 reverse diffusion、reverse SDE 或 flow ODE;
  3. 得到 clean latent formula;
  4. 用冻结的 VAE decoder 转回像素图像:
formula
Latent noise
    -> latent generative dynamics
    -> clean semantic latent
    -> VAE decoder
    -> detailed pixel image

1.5.3 为什么这样分工更合理?

课程把 encoder 类比为 low-pass filter:它压缩高频局部细节,尽量保留 global structure 与 semantic information,让 generation model 在更平滑、更低维的空间里学习。

Decoder 负责把压缩表示扩展为高分辨率图像,并补回 texture、edge 与局部细节。课程称之为一定程度的 texture hallucination:这些低级细节不可能全部逐像素存放在强压缩 latent 中,decoder 必须依据学习到的 image prior 重建它们。

因此,现代 latent generative system 不是要求 diffusion model 独自完成所有事情:

  • Generation model 决定“生成什么”;
  • VAE decoder 决定“怎样把它呈现得细致而真实”。

课程还指出,相关系统中的 decoder 往往比 encoder 更重,因为从低维 latent 恢复高分辨率感知细节是更困难的任务。这个观察属于具体架构设计趋势,不是所有 VAE 都必须满足的数学定律。


2. 多模态条件表示

2.1 怎样表示 Text Condition?

2.1.1 Tokenization

文本首先被拆成 tokens。粒度可以是:

  • arbitrary phrase;
  • word;
  • subword;
  • character。

现代模型常使用 subword tokenization:常见词可以保留为整体,罕见词拆成可复用片段,从而在 vocabulary size 与表达能力之间折中。

设 token sequence 为:

formula

Embedding lookup 把离散 token ID 变成连续向量:

formula

模型还必须加入 position information,否则 attention 本身无法区分 token 顺序:

formula

2.1.2 Attention 的 Query、Key、Value

给定 hidden states formula:

formula

Scaled dot-product attention 为:

formula

它可以理解为:

  1. Query 表示当前位置正在寻找什么;
  2. Query 与所有 Keys 的相似度产生 attention weights;
  3. 用这些 weights 对 Values 加权求和,得到 contextualized representation。

formula 防止维度增大时 dot product 过大,导致 softmax 过度饱和。

2.1.3 Self-Attention 与 Cross-Attention

  • Self-attention:formula 来自同一序列,让每个 token 根据上下文更新表示;
  • Masked self-attention:屏蔽未来 token,适用于 autoregressive decoding;
  • Cross-attention:Query 来自一个模态或网络状态,Key/Value 来自另一组 encoded conditions。

对 text-to-image model 而言,生成网络的 image/latent features 可以作为 Queries,text encoder outputs 作为 Keys 和 Values。Cross-attention 因而让每个空间位置选择性读取 prompt 中的不同 token。

2.1.4 从哪里取 Text Embedding?

可以使用 encoder 最后一层的 token-wise hidden states,也可以使用特殊 token 或最后一个有效 token 对整个句子进行聚合。不同模型的选择不同,但核心要求相同:embedding 应携带 prompt 的 contextual meaning,而不只是孤立 token identity。


2.2 怎样表示 Image Condition?

2.2.1 把 Image Patch 当作 Token

Vision Transformer(ViT)把图像切分成 formula patches。若图像大小为 formula,patch 数量为:

formula

每个 patch 展平并线性投影到 formula 维:

formula

再加入 position embedding,并通常在序列前加入可学习的 formula token:

formula

经过 Transformer encoder 后,formula 对应的 output embedding 可以作为 whole-image representation;patch outputs 则保留更细粒度的 spatial information。

2.2.2 Image Token 与 Text Token 的差别

Text token 通常来自有限 vocabulary,可以直接 lookup embedding。Image patch 是连续值,不能在固定词典中查找,因此需要 linear layer 或 convolution 动态投影。

Patch size 是重要 hyperparameter:

  • Patch 小:保留更多局部细节,但 sequence 更长、attention 成本更高;
  • Patch 大:计算更省,但细粒度结构更容易丢失。

2.2.3 监督 ViT 的限制

如果只用 classification labels 训练 ViT,它会学到任务指定的有限类别表示:

  • 需要昂贵人工标签;
  • 表示受预定义类别约束;
  • 未必捕捉通用视觉语义。

课程简要提到 DINO 等 self-supervised 方法,通过 teacher-student self-distillation 在无人工标签时学习 image representations。但接下来的核心问题更进一步:怎样让 image 与 text 的表示直接可比较?


2.3 CLIP 怎样对齐 Text 与 Image?

2.3.1 Contrastive Learning 的目标

给定成对的 image-caption 数据:

formula

希望:

  • matching image-text pair 在 shared embedding space 中靠近;
  • non-matching pairs 相互远离。

Image encoder 和 text encoder 分别产生表示,再通过 projection heads 映射到相同维度:

formula
formula

使用 cosine similarity:

formula

实际 CLIP 通常再除以 temperature formula,控制 softmax sharpness。

2.3.2 Image-to-Text 概率

固定 image formula,在 batch 内所有 captions 中识别正确文本:

formula

对应 cross-entropy:

formula

2.3.3 Text-to-Image 概率

对称地,固定 caption formula,在 batch 内所有 images 中识别配对图像:

formula
formula

最终 CLIP loss 是双向平均:

formula

2.3.4 In-Batch Negatives

一个 batch 中只有 formula 是 positive pair,其余 formula 个 captions 或 images 都被当作 negatives。这样不需要额外标注负样本,却能构造 formula similarity matrix。

CLIP 原论文使用约 4 亿 image-text pairs,visual encoder 可为 CNN 或 ViT,text encoder 为 Transformer。它展示了自然语言监督可以学到具有强 zero-shot transfer 能力的视觉表示。

2.3.5 CLIP 的计算问题

双向 softmax 需要:

  • 大 batch similarity matrix;
  • 跨设备 global normalization;
  • 较高显存与通信成本。

SigLIP 等后续方法把 batch-level formula softmax 改为每个 pair 独立的 binary sigmoid objective,直接判断“match or not”,从而降低对全局 softmax 的依赖。

在 guided generation 中,我们通常不需要重新训练 CLIP。预训练 text/image encoder 可以直接提供 condition embeddings。


3. Classifier Guidance

设条件为 formula。它可以是 class label,也可以是由 text/image encoder 得到的 embedding。

我们希望从条件反向分布采样:

formula

第一种方法是 Classifier Guidance:使用额外 classifier formula 提供“当前 noisy sample 怎样修改才能更像条件 formula”的梯度。

3.1 Conditional Bayes Rule

忽略与 formula 无关的 normalization constant:

formula

第一项是已有 unconditional reverse model,第二项是 classifier 对条件的判断。

3.2 Unconditional Reverse Transition

DDPM 的一步反向分布写为:

formula

为简化记号,下面写作 formula。

3.3 对 Classifier Log-Probability 做一阶展开

在 formula 附近:

formula

将它与 Gaussian log-density 合并并完成平方,可得到近似条件分布:

formula

最重要的结论是:

Classifier score:

formula

指出怎样改变当前样本,才能提高目标类别 formula 的 log-probability。

3.4 Guidance Scale

实际中通常放大 condition signal:

formula

课程指出,经验上常需要 formula 才能获得较强的 condition adherence。

3.5 Classifier Guidance 的优点与限制

优点:已有 unconditional generation model 不需要重新训练,guidance 是 sampling-time modification。

代价:

  1. 必须有 labeled data;
  2. classifier 必须在各 noise levels 的 noisy images 上训练,普通 clean-image classifier 不能直接使用;
  3. classifier 与 generation model 的数据分布需要对齐;
  4. 每一步都需要 classifier forward 和对输入的 backward gradient;
  5. guidance scale 需要调节;
  6. 均值平移依赖 first-order Taylor approximation。

这促使课程提出下一问:能不能从 generation model 自己获得 condition direction,而不再维护额外 classifier?


4. Classifier-Free Guidance

很清晰的讲解: https://zhuanlan.zhihu.com/p/660518657****

4.1 Conditional 与 Unconditional 模型隐含一个 Classifier

由 Bayes' rule:

formula

取 log gradient:

formula

右侧正是 conditional score 与 unconditional score 的差:

formula

因此,只要同一个 generation network 能同时预测 conditional 与 unconditional signal,它们的差就提供了 implicit classifier direction。

4.1.1 从 Score 写到 Noise Prediction

对常见 Gaussian perturbation:

formula

Classifier-based noise guidance 可写为:

formula

用 conditional/unconditional score difference 替换 classifier gradient,得到本讲采用的 CFG 公式:

formula

等价写法为:

formula

当:

  • formula:完全 unconditional;
  • formula:普通 conditional prediction;
  • formula:越过 conditional prediction,沿 condition direction 外推。

这最后一点很重要:CFG 不是在 conditional 与 unconditional 之间简单插值,实际常用的 formula 是 extrapolation。

4.1.2 为什么其他资料会出现不同公式?

有些资料定义:

formula

它等价于本讲公式,只要:

formula

因此看到 formula、formula 或不同默认值时,必须先检查作者把 guidance scale 定义在哪个基点,不能直接比较数字。


4.2 CFG 怎样训练?

4.2.1 Condition Dropout

同一个网络必须同时学会:

formula

和:

formula

训练时以概率 formula 丢弃条件,用 null condition formula 替代;其余样本保留条件。

4.2.2 训练步骤

采样:

formula

构造 noisy sample:

formula

随机决定使用的条件:

formula

为了 CSDN 显示稳定,也可把它直接理解为一次 Bernoulli condition dropout。

训练 loss:

formula

课程给出的常用经验范围为:

formula

如果无条件样本太少,unconditional branch 学不好,CFG difference 不可靠;如果太多,conditional learning capacity 又会被稀释。

4.2.3 推理仍需要两次 Forward Pass

每个 timestep 需要分别计算:

formula
formula

再组合得到 formula。因此 classifier-free 的意思是“不需要独立 classifier”,并不是“guidance 没有额外计算”。标准 CFG 仍然需要每步两次 generation-network evaluation,实践中常把两份输入拼成一个 batch 并行计算。

4.2.4 Guidance Scale 的实际权衡

formula 增大通常会:

  • 提高 prompt/class adherence;
  • 提高某些样本的清晰度和辨识度;
  • 降低 diversity;
  • 在过大时引入过饱和、失真和 artifacts。

Guidance scale 不是越大越好。它控制的是“服从条件”与“保留数据分布自然性”之间的权衡。


5. 条件生成系统

这一讲主要讨论 condition representation 与 guidance,不深入下一讲的完整网络架构。可以先把系统分成两个层面。

5.1 Conditioning

Text encoder 或 image encoder 产生 embedding:

formula

生成网络通过 cross-attention、feature modulation 或其他条件注入模块读取 formula:

formula

它回答:条件内容怎样进入网络?

5.2 Guidance

CFG 用 conditional 与 unconditional prediction 的差放大条件影响:

formula

它回答:采样时要多强地服从条件?

Conditioning 与 guidance 不是一回事。即使 formula,模型也已经是 conditional model;CFG 只是进一步调整 conditional strength。


5.3 把整套 Multimodal Guided Generation 系统串起来

以 text-to-image latent diffusion 为例。

5.3.1 预训练表示模块

  1. 训练 VAE,使图像可以在 pixel space 与 latent space 之间转换;
  2. 使用预训练 text encoder,把 prompt 转成 contextual embeddings;
  3. 可使用 CLIP-style training,让 text/image 表示具有共享语义。

5.3.2 训练条件生成模型

  1. 用冻结 VAE encoder 把 image 转成 latent formula;
  2. 对 formula 加噪得到 formula;
  3. 用 text embedding 作为 condition;
  4. 随机丢弃一部分 condition,使同一网络同时学习 conditional 与 unconditional prediction;
  5. 优化 noise、score 或 velocity regression loss。

5.3.3 CFG 推理

Prompt
  -> tokenizer
  -> text encoder
  -> condition embeddings
                      \
Latent noise           -> conditional/unconditional generator calls
                      -> CFG combination
                      -> latent denoising trajectory
                      -> clean latent
                      -> VAE decoder
                      -> pixel image

这也解释了前三讲与本讲的关系:DDPM、Score Matching、Flow Matching 决定 latent state 怎样演化;Lecture 4 增加 representation 与 condition direction,使这条演化轨迹能够按照用户意图进行。


6. 总结与速查

6.1 图像空间链

Pixel space 太高维且几何尖锐
        -> AutoEncoder 压缩
        -> VAE 用 KL 结构化 latent space
        -> Perceptual + Adversarial loss 改善重建
        -> 在 latent space 训练生成模型

6.2 条件表示链

Text -> tokenization -> Transformer embeddings
Image -> patches -> ViT embeddings
        -> CLIP contrastive alignment
        -> shared multimodal semantic space

6.3 Guidance 链

Conditional Bayes rule
        -> classifier score 平移 reverse mean
        -> Classifier Guidance
        -> conditional - unconditional = implicit classifier signal
        -> Classifier-Free Guidance

6.4 系统分工链

VAE encoder       压缩图像,提供可学习 latent
Condition encoder 表示用户意图
Generation model  学习 latent dynamics
CFG               放大 condition direction
VAE decoder       恢复像素与高频细节

6.5 建议记住的 10 个公式

6.5.1 VAE posterior sampling

formula

6.5.2 VAE loss

formula

6.5.3 Refined VAE loss

formula

6.5.4 Latent noising

formula

6.5.5 Scaled dot-product attention

formula

6.5.6 CLIP similarity

formula

6.5.7 CLIP symmetric loss

formula

6.5.8 Classifier-guided reverse mean

formula

6.5.9 Implicit classifier score

formula

6.5.10 Classifier-Free Guidance

formula

6.6 容易混淆的概念

  1. Latent Diffusion 不是 VAE 自己在生成最终分布。 VAE 负责表示与解码,diffusion/flow model 负责学习复杂 latent distribution。
  2. AE 的低维不等于有结构。 Reconstruction alone 不保证 latent geometry 适合 Gaussian sampling。
  3. VAE prior 与 posterior 不同。 formula 是固定 prior,formula 是 encoder 输出的 approximate posterior。
  4. KL 不是越大越好。 过强会造成 posterior collapse。
  5. Pixel fidelity 与 perceptual quality 不相同。 Pixel MSE 很小不一定代表人眼看来锐利自然。
  6. Perceptual loss 与 semantic embedding 也不相同。 前者偏重局部视觉观感,后者偏重全局意义。
  7. Decoder 恢复细节不等于细节原样储存在 latent 中。 强压缩后的一部分 texture 由 decoder 根据 learned prior 重建。
  8. Text token 与 image patch token 形成方式不同。 前者通常 vocabulary lookup,后者是连续 patch projection。
  9. CLIP 做的是跨模态 alignment。 它不是 image generator。
  10. CLIP embedding 是 condition representation,不等于 guidance 本身。 Guidance 还要改变生成网络的 sampling direction。
  11. Classifier Guidance 需要 noisy classifier。 普通 clean-image classifier 的输入分布不匹配。
  12. Classifier-Free 不等于 unconditional。 它用一个同时支持 conditional/unconditional 的模型推断隐式 classifier direction。
  13. CFG 通常每步仍需两次网络预测。 消失的是独立 classifier 和 classifier backward pass。
  14. formula 是外推而不是插值。 它把 prediction 推过普通 conditional point。
  15. 不同实现中的 guidance scale 定义可能相差 1。 比较参数前先检查公式。
  16. 更强 guidance 不总是更好。 Prompt adherence、quality 与 diversity 之间存在权衡。

6.7 一张最终知识图

Pixel image x
     |
     v
VAE Encoder ---------------------------+
     |                                 |
latent z_0                             | frozen representation model
     |                                 |
add noise -> z_t                       |
     |                                 |
     +------ Generation Network <------+------ Condition Embedding
                    |                              ^
                    |                              |
          conditional prediction          Text Transformer / Image ViT
          unconditional prediction                 ^
                    |                              |
                    +------ CFG ------------------ Prompt / Reference image
                              |
                       clean latent z_0
                              |
                         VAE Decoder
                              |
                       generated image

6.8 最简总结

  • Pixel Space:直观但高维、冗余,并且普通欧氏几何缺乏语义;
  • AutoEncoder:通过 reconstruction bottleneck 压缩图像,但不保证 latent structure;
  • VAE:用 approximate posterior 与 KL regularization 构造平滑、可采样的 latent space;
  • Perceptual Loss:在 feature space 比较重建,减少 pixel alignment 带来的模糊倾向;
  • Adversarial Loss:用 discriminator 促进更锐利、真实的 decoder output;
  • Latent Diffusion:把昂贵的生成动力学移到低维 latent space,最后再由 VAE decoder 恢复图像;
  • Transformer / ViT:分别把 text tokens 与 image patches 编码为 contextual embeddings;
  • CLIP:用双向 contrastive objective 对齐 text 和 image 的 shared semantic space;
  • Classifier Guidance:用 noisy classifier score 平移 reverse transition;
  • Classifier-Free Guidance:用 conditional 与 unconditional predictions 的差构造 implicit classifier direction;
  • Guidance Scale:控制条件一致性与自然度、多样性的权衡。
posted on 2026-09-07 15:53  牛牛的智驾笔记  阅读(9)  评论(0)    收藏  举报