Theme
下面是 Lecture 23–25 的复习笔记总纲:这三讲的主线是从“生成模型如何生成连续数据”走到“扩散模型如何用于文本”,再走到“LLM 如何从单次生成模型扩展为 agentic system”。内容依据三份课件整理:Lecture 23 diffusion fundamentals,Lecture 24 score-based/text diffusion,Lecture 25 agentic systems。
0. 三讲的核心进化树
text
Generative Models
├── GAN / VAE:一次性从 latent 生成样本
│ ├── 优点:采样快
│ └── 痛点:mode coverage / fidelity / likelihood 训练等问题
│
├── Diffusion Models:多步迭代 refinement
│ ├── Forward process:逐步加噪,把 data → Gaussian noise
│ ├── Reverse process:学习逐步去噪,把 noise → data
│ ├── DDPM view:预测 noise ε
│ └── Score-based view:学习 score ∇x log p(x)
│
├── Text Diffusion
│ ├── 离散 token diffusion:transition matrix Q_t
│ ├── continuous embedding diffusion:Diffusion-LM
│ └── controlled generation:classifier guidance / gradient control
│
└── Agentic Systems
├── LLM 不是只做一次生成,而是作为 brain
├── Memory:长期/短期记忆
├── Tools:搜索、代码、GUI、外部 API
├── Reflection:CoT / LLM-as-a-judge
└── Collaboration:multi-agent / MCP重要程度标注: ★★★ = 期末核心;★★ = 应理解;★ = 了解即可。
1. Lecture 23:Diffusion Models 基础
1.1 本讲核心目标 ★★★
Lecture 23 要解决的问题是:如何把“从随机噪声生成高质量样本”变成一个可训练、可推导、可采样的概率模型。
GAN/VAE 通常试图一步完成:
但 diffusion 的核心思想是:
先人为设计一个 forward noising process 把数据破坏成标准高斯噪声,再学习 reverse denoising process:
它的本质不是“一步生成”,而是“多步修复”。
1.2 生成模型三难困境 ★★
课件给出 generative trilemma:
text
sample fidelity high-quality samples
fast sampling generation speed
mode coverage diversityGAN、VAE、Diffusion 的差异:
| 模型 | 生成方式 | 优点 | 痛点 |
|---|---|---|---|
| GAN | 一步从噪声到样本 | 采样快、样本锐利 | mode collapse,训练不稳定 |
| VAE | encoder-decoder latent generation | 有 likelihood/ELBO 框架 | 样本容易模糊 |
| Diffusion | 多步去噪 | fidelity 和 diversity 强 | 采样慢 |
GAN 目标:
符号说明: :generator,把噪声 映射到数据空间; :discriminator,判断样本是真实数据还是生成数据; :真实数据分布; :latent noise prior。
VAE 目标:
符号说明: :encoder,把数据 映射到 latent distribution; :decoder,从 latent 重构 ; :latent prior,通常是 ; KL 项约束 posterior 不要偏离 prior。
Diffusion 的核心是 reverse Markov chain:
其中:
1.3 Forward stochastic process ★★★
Forward process 是一个人为定义、无参数的 Markov chain:
也常写为:
其中:
符号说明: :原始 clean data; :第 步加噪后的样本; :variance schedule,即第 步加入多少噪声; :signal retention,即保留多少原信号; :单位协方差矩阵; :固定的 forward noising distribution。
物理直觉:墨水滴入水中。初始图像 是低熵、有结构的状态;随着加噪,结构逐渐消失,最终 接近高熵的 isotropic Gaussian noise。
1.4 为什么要有 ?★★★
Forward step 可以写成 reparameterization:
如果不用 缩放原信号,连续加噪会导致 variance 不受控。这个设计保证每一步的方差保持稳定。
从两步推导:
代入:
展开:
两个独立 Gaussian 相加仍是 Gaussian:
所以噪声方差合并为:
因此:
推广到任意 ,定义:
得到关键公式:
等价采样式:
这是 diffusion 训练能高效进行的关键,因为训练时不需要真的从 一步步加噪到 ,可以直接采样任意 。
1.5 Variance schedule ★★
定义:
控制 在 中还剩多少信号。 当 增大时:
因此:
linear schedule 的问题是信号可能太早崩掉;cosine schedule 会让信号保留更久,避免后期突然 collapse。
1.6 Reverse process:从 noise 回到 data ★★★
目标是学习:
通常设为 Gaussian:
问题:真实 reverse posterior
是不可解的,因为根据 Bayes:
其中 要对整个数据空间积分:
这是 intractable。
但是如果额外 conditioning on clean data ,则:
由于这些都是 forward process 中已知的 Gaussian,所以 posterior tractable。
1.7 Tractable posterior mean ★★★
真实可解 posterior 是:
其中 posterior mean 为:
符号说明: :真实 posterior 的均值; :真实 posterior 的方差; :当前 noisy sample; :clean data; :前 步的累计 signal retention; :第 步 noise variance。
直觉: 是 和 的 time-dependent interpolation。早期 信号较多,posterior 更依赖 ;后期噪声多,需要更多依赖 。
1.8 为什么预测 noise 等价于预测 reverse mean?★★★
在 inference 中, 不知道。但由 forward marginal:
可解出:
用 neural network 预测噪声:
代入 posterior mean,得到 DDPM 的 reverse mean:
这是全章最核心公式之一。它说明:模型不需要直接预测 ,也不需要直接预测 ,只要预测当初加进去的 noise ,就能构造 reverse transition 的 mean。
1.9 ELBO / variational objective ★★★
目标是最大化 data likelihood:
等价最小化 negative log likelihood:
但:
不可直接算。因此引入 variational distribution:
得到 variational bound:
定义 VLB:
展开为:
进一步分解成:
更标准地写:
三项含义:
| 项 | 名称 | 意义 |
|---|---|---|
| prior loss | 约束最终噪声接近 | |
| denoising loss | 让模型 reverse posterior 逼近真实 posterior | |
| reconstruction loss | 从 重构 |
1.10 Simplified loss ★★★
两个 Gaussian KL:
当方差固定时,KL 就变成 mean 的 L2 距离。Ho et al. 进一步发现,直接训练 noise prediction 的 unweighted MSE 效果更好:
其中:
这就是 DDPM 训练的常用目标。
1.11 Training algorithm ★★★
每次训练:
text
1. sample x0 ~ data
2. sample t ~ Uniform({1,...,T})
3. sample ε ~ N(0,I)
4. construct xt = sqrt(αbar_t)x0 + sqrt(1-αbar_t)ε
5. predict εθ(xt,t)
6. minimize ||ε - εθ(xt,t)||²核心优点:训练时只需要一次 forward noising,不需要真的跑完整 步。
1.12 Sampling algorithm ★★★
采样从 Gaussian noise 开始:
然后对 :
其中:
最后得到 。
1.13 U-Net and time embedding ★★
Diffusion 网络通常用 U-Net:
text
input noisy image xt
↓
downsampling path:提取 global structure
↓
bottleneck:全局语义
↓
upsampling path:恢复 spatial resolution
↓
skip connections:保留细节因为每个 timestep 的噪声强度不同,模型必须知道当前 。因此加入 time embedding:
通常是 sinusoidal embedding,再注入每个 residual block。
2. Lecture 24:Score-based View 与 Text Diffusion
2.1 本讲核心目标 ★★★
Lecture 24 解决两个问题:
第一,DDPM 为什么可以从另一个角度理解成 score matching? 第二,diffusion 如何迁移到 NLP/text generation?
核心转变:
text
DDPM view:
predict noise ε
Score-based view:
predict score ∇x log p_t(x)
Text diffusion:
continuous Gaussian noise 不适合 discrete token
→ discrete transition matrix 或 embedding-space diffusion2.2 Score function ★★★
score function 定义为:
符号说明: :数据分布; :log-density; :指向概率密度上升最快方向的向量场。
直觉: 如果一个 noisy sample 落在低密度区域,score 会告诉它应该往哪里移动,才能回到高密度 data manifold。
2.3 Langevin dynamics ★★★
Langevin dynamics 用 score 来采样:
符号说明: :第 次迭代的 sample; :step size; :score,引导样本走向高密度区; :随机扰动,防止陷入局部 mode。
物理直觉: 令 potential energy:
那么最大化 等价于最小化 。Langevin dynamics 是带随机热扰动的 gradient descent。
2.4 为什么 score unknown?★★★
真实数据分布 不知道,所以:
不可直接算。于是训练神经网络:
理想 loss:
但 target unknown,因此需要 denoising score matching。
2.5 Denoising score matching ★★★
对 clean data 加 Gaussian noise:
因此:
取 log:
对 求梯度:
由于:
所以:
Denoising score matching loss:
代入:
这说明预测 score 与预测 noise 等价:
2.6 DDPM vs Score-based view ★★★
| 维度 | DDPM view | Score-based view |
|---|---|---|
| 学什么 | noise | score |
| 采样 | reverse Markov chain | Langevin / SDE |
| 时间 | discrete timesteps | continuous-time limit |
| 核心公式 | ||
| 关系 | 与 score 成比例 | 等价视角 |
核心连接:
2.7 为什么 NLP 需要 diffusion?★★
Autoregressive generation 的问题:
text
left-to-right generation
前面生成错了,后面只能在错误 prefix 上继续
global constraint 难以提前规划例如要求句子最后必须包含某个词,AR 模型到最后才发现约束,但前面上下文已经固定。
Diffusion 的优势:
text
whole-sequence refinement
所有 token 同时被反复修改
早期确定 global semantics
后期修复 local syntax因此 diffusion 更适合 global constraint、style control、syntax control 等任务。
2.8 Text diffusion 的核心困难 ★★★
图像像素虽然也是离散存储,但可以自然视为连续变量:
token 不是连续变量:
其中 是 vocabulary。问题是:
text
token id 之间没有自然距离
cat 和 dog 的 id 差距没有语义意义
对 token id 加 Gaussian noise 会落到无效 token所以不能直接:
2.9 Discrete diffusion:transition matrix ★★★
在 discrete vocabulary 上,用 transition matrix 替代 Gaussian kernel:
forward process:
如果 是 one-hot token vector,那么 是下一个 token 的 categorical distribution。
三种 corruption:
Keep
其中: :identity matrix,保留原 token; :uniform transition matrix; :noise level。
Mask / absorbing diffusion
其中: :[MASK] token 的 one-hot vector; :all-ones vector。
含义:token 有概率被替换成 [MASK],而 [MASK] 是 absorbing state。
Swap / uniform diffusion
含义:token 被随机替换成 vocabulary 中其他 token。
2.10 Discrete forward posterior ★★★
定义累计 transition:
Forward likelihood:
Posterior via Bayes:
用 matrix 写:
训练 reverse model:
KL objective:
对于 categorical distribution,KL minimization 等价于 cross-entropy:
2.11 Discrete diffusion 的痛点 ★★
如果 vocabulary 很大:
矩阵非常大,计算和存储都昂贵。因此引入 continuous latent diffusion / Diffusion-LM。
2.12 Diffusion-LM:continuous embedding diffusion ★★★
把 token 映射到 embedding space:
给定句子:
编码为:
然后在 continuous embedding space 上做 Gaussian diffusion:
训练目标包含 diffusion loss 和 rounding loss。
Diffusion loss:
Rounding 到 token:
整体 loss 可理解为:
直觉: 模型不仅要把 noisy embedding denoise 回 clean embedding,还要保证 clean embedding 能正确 decode 成离散词。
2.13 Controlled generation ★★★
目标是生成满足控制条件 的文本:
在每一步 reverse diffusion 中:
采用条件独立假设:
因此:
取 log:
对 求梯度:
第一项来自 diffusion model,保证 fluency;第二项来自 classifier,推动样本满足 control constraint。
2.14 Classifier training ★★
给定 labeled dataset:
训练步骤:
text
1. sample (w,c)
2. map text to embedding x0
3. sample timestep t
4. construct xt = sqrt(αbar_t)x0 + sqrt(1-αbar_t)ε
5. classifier predicts c from xt and t
6. minimize cross entropyloss:
该 classifier 学会从 noisy embedding 中恢复 control label。
2.15 Block Diffusion ★★
普通 discrete diffusion 的问题:
text
fixed-length generation
不能像 GPT 一样自然任意长度生成
无法复用 KV-cacheBlock Diffusion 在 AR 和 diffusion 之间折中:
其中每个 block 内部用 diffusion 并行去噪,block 之间 autoregressive。
优势:
| 模型 | 高质量 | 任意长度 | KV-cache | 并行性 |
|---|---|---|---|---|
| AR | 强 | 强 | 强 | 弱 |
| Diffusion | 可并行 | 固定长度 | 弱 | 强 |
| Block Diffusion | 折中 | 强 | 可用 | block 内并行 |
3. Lecture 25:Agentic Systems
3.1 本讲核心目标 ★★★
Lecture 25 的目标是解释:为什么 LLM 不再只是一个“输入 prompt 输出文本”的模型,而是逐渐变成 agentic system 的核心组件。
核心进化:
text
LLM as model
只做被动生成
↓ 加 planning / memory / tools / feedback
LLM as agentic system
能多步执行、记忆历史、调用工具、反思错误、与环境交互3.2 Agentic system 定义 ★★★
一个 agentic system 通常包括:
text
LLM brain:
planning, reasoning, rethinking
Memory:
persistent perception and experience
trajectory, feedback, user preferences
Tools:
execute actions in external environments
search, code, APIs, GUI
Environment:
state transition, observation, reward, feedbackAgent 的本质不是模型本身,而是模型外面的一整套 harness / scaffold。
3.3 LLM 与 agentic system 的区别 ★★★
| 能力 | 普通 LLM | Agentic system |
|---|---|---|
| 交互方式 | 一问一答 | 多轮行动 |
| 规划 | 隐式、短程 | 显式 multi-step planning |
| 记忆 | context window 内 | persistent memory |
| 工具 | 无 | 可调用搜索、代码、API、GUI |
| 反馈 | 用户下一轮反馈 | environment reward / observation |
| 错误修正 | 难 | reflection / retry |
| 协作 | 单模型 | multi-agent collaboration |
普通 LLM 的限制:
text
passive generation
limited context window
memory degradation
only forward text generation
no real environment interactionAgentic system 试图解决:
text
complex task decomposition
execution history tracking
tool-augmented perception/action
reflection after failure
collaboration among agents3.4 Agent 示例 ★★
Robotics
机器人 agent 需要:
text
camera perception
segmentation model
3D scene graph memory
navigation actions
manipulation tools例如 ConceptGraph 用 3D scene graph 作为环境记忆。这里 memory 不是聊天记录,而是 structured environment representation。
Electricity trading
电力交易 agent 需要:
text
weather forecasting
grid status checking
market demand prediction
trading rules
historical execution feedback其 plan 可能比较固定,但参数需要根据经验调整。
3.5 Agentic system 与 computer system 类比 ★★
课件把 LLM agent 类比成 computer:
| Computer | LLM Agent |
|---|---|
| CPU | LLM weights |
| RAM | context window |
| hard disk | vector DB / long-term storage |
| device drivers | tool integrations |
| OS | agent harness |
| applications | agent-defined behaviors |
直觉: LLM 是“计算核心”,但真正完成任务需要 memory、tool、controller、environment loop。
3.6 Context engineering ★★★
Context engineering 是优化送入 LLM 的上下文。
主要问题:
text
context window limited
lost in the middle
irrelevant/conflicting retrieved information
context conflicts with parametric knowledge
rich structures like graphs/images cannot be fully serialized as tokens尤其 “lost in the middle” 指的是模型对长上下文中间部分的信息利用较差。
Agentic system 的改进方式:
text
把所有信息塞进 prompt
→ 改成检索、压缩、结构化、工具调用、分阶段 reasoning3.7 Memory ★★★
Memory 分为 long-term 和 short-term。
Long-term memory:
text
explicit/declarative:事实、规则、用户偏好
episodic:过去事件、交互轨迹
procedural:技能、习惯、操作流程
implicit:从经验中形成的行为模式Short-term memory:
text
context window:当前输入和对话
working memory:当前任务中的中间状态、计划、变量全局行为由长期记忆定义,局部行为由短期记忆定义。
3.8 RAG ★★★
RAG = Retrieval-Augmented Generation。
它解决:
text
LLM knowledge outdated
information source not transparent
hallucination
parametric memory 不可靠RAG 三步:
text
1. Indexing:
build indexed database for documents
2. Retrieval:
retrieve top-k relevant chunks
3. Generation:
insert chunks into LLM context and generate answerRAG 的核心不是“模型记住了知识”,而是“模型在回答前查阅外部 memory”。
3.9 Reflection ★★
Reflection 包括 CoT 和 LLM-as-a-judge。
CoT:
作用:中间步骤提供更多 token 和约束,让后续生成更稳定。
LLM-as-a-judge:
text
main LLM output
↓
judge LLM evaluates quality
↓
score / critique / retry它把“自我评估”变成 agentic loop 的一个模块。
3.10 Tools ★★★
Agent tools 包括:
text
web search
code interpreter
PDF parser
file manipulation
GUI operation
external APIs什么时候需要 web search:
text
query requires current information
LLM context insufficient
beyond model learned knowledge
需要来源透明性GUI agent 的关键动作:
text
recognize screen state
locate clickable region
type content
execute action
observe result3.11 Collaboration / MCP ★★
MCP 的思想是统一模型和工具之间的接口。
如果有 个模型和 个服务,传统集成需要:
种交互方式。统一协议后只需要:
这降低了系统集成复杂度。
4. 三讲总对比:从 Diffusion 到 Agent
| 主题 | 核心问题 | 方法 | 关键公式/机制 | 重要性 | |
|---|---|---|---|---|---|
| DDPM | 如何从噪声生成数据 | forward + reverse diffusion | ★★★ | ||
| Reverse mean | 如何去噪 | predict noise | ★★★ | ||
| Simplified loss | 如何训练 | MSE noise prediction | ★★★ | ||
| Score-based | diffusion 的另一个解释 | learn score | ★★★ | ||
| Langevin | 如何用 score 采样 | gradient + noise | ★★★ | ||
| Text diffusion | token 不连续怎么办 | discrete Q 或 embedding diffusion | (q(x_t | x_{t-1})=\mathrm{Cat}(Q_tx_{t-1})) | ★★★ |
| Diffusion-LM | 避免巨大 transition matrix | embedding-space Gaussian diffusion | ★★★ | ||
| Agent | LLM 如何完成复杂任务 | planning + memory + tools + environment | controlled while loop | ★★★ |
5. 期末最可能考的核心点
第一,必须能推导 forward marginal:
第二,必须理解为什么 prediction noise 等价于 prediction reverse mean:
第三,必须能解释 simplified loss:
第四,必须知道 score function:
以及它和 noise prediction 的关系:
第五,必须知道 text diffusion 为什么困难:token 是 discrete categorical variable,没有自然连续距离。
第六,必须掌握 discrete diffusion transition matrix:
第七,agentic system 的四件套:
text
LLM brain + memory + tools + environment以及 RAG 三步:
text
indexing → retrieval → generation