《AI Engineering》第2章 – 理解基础模型
“In general, however, differences in foundation models can be traced back to decisions about training data, model architecture and size, and how they are post-trained to align with human preferences”(摘录原文)
💬 评论:基础模型应该关注的参数:训练的数据,模型架构,规模,如何进行后期训练使其符合人类偏好等方面的决策
“Since models learn from data, their training data reveals a great deal about their capa‐ bilities and limitations. This chapter begins with how model developers curate train‐ ing data, focusing on the distribution of training data. Chapter 8 explores dataset engineering techniques in detail, including data quality evaluation and data synthesis.”(摘录原文)
💬 评论:模型数据的一些概念:训练数据的选择,训练数据的分布,数据集工程(数据质量评估,数据合成)
训练数据的选择(重点:训练数据的分布)
“Some might wonder, why not just train a model on all data available, both general data and specialized data, so that the model can do everything? This is what many people do. However, training on more data often requires more compute resources and doesn’t always lead to better performance. For example, a model trained with a smaller amount of high-quality data might outperform a model trained with a large amount of low-quality data. Using 7B tokens of high-quality coding data, Gunasekar et al. (2023) were able to train a 1.3B-parameter model that outperforms much larger models on several important coding benchmarks. The impact of data quality is dis‐ cussed more in Chapter 8.”(摘录原文)
💬 评论:训练数据的选择并非越多越好,更多的数据意味着就要更多的训练成本,而得到的结果并非会有更好的性能,而且很多从互联网公开信息获取的数据有真有假,质量良莠不齐。所以训练数据的数量和训练数据的质量都是影响结果的因素
💡 思考:训练数据的质量和数量,还有多语言的接收也会影响模型生成的质量以及模型消耗的 token,还有特定领域的模型,通用型可以对日常一些问题做出回答,但是对于信息在互联网上相对少披露的领域回答的准确率也会下降
Transformer 架构特性以及什么样的架构可以取代
“As of this writing, the most dominant architecture for language-based foundation models is the transformer architecture (Vaswani et al., 2017), which is based on the attention mechanism. It addresses many limitations of the previous architectures, which contributed to its popularity.”(摘录原文)
💬 评论:transformer 架构基于注意力机制,这也是transformer受欢迎的原因
💡 思考:1.什么是注意力机制,2.之前的架构有那些局限性
🤖 From GPT: 这张图展示了传统的 Seq2Seq (RNN-based) 架构 和 Transformer 架构在处理翻译任务(如从英文 “How are you?” 翻译成西班牙语 “¿Cómo > estás?”)时的 核心差异。
我们逐部分解释:
⸻
🔲 上半部分:Seq2Seq(基于 RNN 的架构)
✅ 工作原理:
- 输入序列 "How are you ?" 被一个 编码器 RNN 处理,最后得到一个 最终的隐藏状态(Final hidden state)。
- 解码器 RNN 从这个隐藏状态出发,逐步生成目标语言的单词: • 生成 ¿ • 然后是 Como • 然后 estas • 最后是 ?
- 所有的翻译决策都依赖于“最终隐藏状态”这个压缩的信息向量。
❗ 问题: • 信息瓶颈(信息压缩):长句子时,这个隐藏状态难以保留足够的信息。 • 上下文丢失严重:早期输入(如 “How”)可能在最后状态中被淡化或忽略。
⸻
🔲 下半部分:Transformer(基于 Attention 机制)
✅ 工作原理:
- 输入同样是 "How are you ?",但 Transformer 不是将所有信息压缩进一个隐藏状态,而是对每个输出词都: • “关注”输入序列中的不同词(attention)。 • 如图中,生成 ¿ 时,主要参考了 “How”;生成 estas 时则参考了 “you”。
- 彩色箭头 表示的是 attention: • 每个输出词(西语单词)根据 attention 去看输入句中哪些词重要。 • 例如: • ¿ 主要关注 “How” • Como 同时参考 “How”、“are” • estas 更多关注 “are” 和 “you” • 最后的 ? 关注整个输入句
✅ 优势: • 无压缩瓶颈:每个输出词都能访问整个输入句。 • 并行计算:Transformer 非序列生成,可并行训练。 • 更好的上下文建模能力。
⸻
📌 总结对比
特性 Seq2Seq (RNN) Transformer 输入信息获取 仅靠最后隐藏状态 全局 attention 长句建模能力 弱 强 训练效率 慢,串行处理 快,可并行 可解释性 低 高(通过 attention 可视化) ⸻
💡应用启发:
这张图很好地说明了为什么 Transformer 架构在机器翻译、ChatGPT 等任务中表现远胜于传统 RNN 模型。
如果你正在学习 NLP 或 transformer 架构,这张图可以作为理解 attention 工作机制的经典范例。
“There are two problems with seq2seq that Vaswani et al. (2017) addresses. First, the vanilla seq2seq decoder generates output tokens using only the final hidden state of the input. Intuitively, this is like generating answers about a book using the book summary. This limits the quality of the generated outputs. Second, the RNN encoder and decoder mean that both input processing and output generation are done sequentially, making it slow for long sequences. If an input is 200 tokens long, seq2seq has to wait for each input token to finish processing before moving on to the”(摘录原文)
💬 评论:seq2seq(序列到序列)存在的2个问题:
- 普通的序列到序列解码器仅使用输入的最终隐藏状态来生成输出标记。直观地说,这就像是根据书籍的内容梗概来生成有关这本书的答案。这限制了生成输出的质量
- 循环神经网络(RNN)编码器和解码器意味着输入处理和输出生成都是按顺序进行的,这使得处理长序列时速度较慢。如果一个输入有 200 个标记长,序列到序列模型必须等待每个输入标记处理完毕后才能继续处理下一个。
“The transformer architecture addresses both problems with the attention mecha‐ nism. The attention mechanism allows the model to weigh the importance of differ‐ ent input tokens when generating each output token. This is like generating answers by referencing any page in the book. A simplified visualization of the transformer architecture is shown in the bottom half of”(摘录原文)
💬 评论:Transformer架构通过注意力机制解决了这两个问题。注意力机制使模型在生成每个输出标记时,能够权衡不同输入标记的重要性。这就好比通过查阅书中的任意一页来生成答案。上图的下半部分展示了Transformer架构的简化示意图。
“While the attention mechanism is often associated with the trans‐ former model, it was introduced three years before the transformer paper. The attention mechanism can also be used with other archi‐ tectures. Google used the attention mechanism with their seq2seq architecture in 2016 for their GNMT (Google Neural Machine Translation) model. However, it wasn’t until the transformer paper showed that the attention mechanism could be used without RNNs”(摘录原文)
💬 评论:注意力机制并不和 transformer 架构强绑定。
“The attention mechanism computes how much attention to give an input token by performing a dot product between the query vector and its key vector. A high score means that the model will use more of that page’s content (its value vector) when generating the book’s summary. A visualization of the attention mechanism with the key, value, and query vectors is shown in Figure 2-5. In this visualization, the query vector is seeking information from the previous tokens How, are, you, ?, ¿ (\dot{c}) to gen‐ erate the next token.”(摘录原文)
💬 评论:注意力机制通过计算查询向量与其键向量之间的点积,来确定对输入词元应给予多少关注。分数越高,意味着模型在生成书籍摘要时会更多地使用该页面的内容(其值向量)(类似权重的概念)。 📚 参考
Transformer 中 Attention 机制详细解析
这张图是经典 Transformer 中 Attention 机制的详细流程图,来源于论文《Attention is All You Need》——是理解 Transformer > 的关键之一。
我会将这张图分为 左右两个部分 来逐步详细讲解。
🧩 左侧部分:Attention 的计算流程
这是标准的 Scaled Dot-Product Attention 计算流程,用来根据输入的 Query(Q)、Key(K)、Value(V) 计算“关注什么”。
🔢 步骤拆解如下:
输入向量:Q、K、V
- Query(查询向量)Q
- Key(键向量)K
- Value(值向量)V
这些向量都从输入 token(比如词嵌入)经过不同线性变换得到。第一个 MatMul(Q × Kᵀ)
- 将 Q 与所有 K 进行点积,得到相似度得分矩阵(attention score)
- 表示当前 token 与上下文中所有 token 的相关性
Scale 缩放(除以 √dₖ)
- 为了防止值过大导致梯度消失,通常将得分除以 √(key 的维度 dₖ)
Mask(可选)
- 在解码时,为了防止模型“偷看未来 token”,对未来位置加负无穷(-inf)
Softmax
- 将得分转换为概率分布,表示“当前 token 应该关注各个位置的程度”
第二个 MatMul(attention weights × V)
- 将每个 V(value)按注意力权重加权求和,得到最终的 attention 输出
🧩 右侧部分:生成下一个 token 时的注意力过程
这部分更直观地展示了:在解码时,模型是如何根据前面的 token 计算下一个 token 的。
📌 场景背景:
模型已经看到了前面的 token:
How / are / you / ? / ¿
现在要生成下一个词("Como")。
✅ 核心流程:
每个已知 token(How, are, …)都会生成自己的 K 和 V 向量
- 如图中橙色部分:Key 向量 K1, K2, ..., Kt
- 绿色部分:Value 向量 V1, V2, ..., Vt
当前要生成的 token(“Como”)构造自己的 Query 向量 Qt
- 红色框中是 Qt:Query for token t+1
Query Qt 与所有 Key 进行点积 → 得到相似度分数
- 可以理解为“Como”现在在问:“我应该关注谁?”
根据相似度 + softmax → 得到权重(attention weight)
- 比如它可能关注“¿”最多,其次是 “you”
根据权重加权所有 Value 向量
- 得到一个综合向量 → 表示当前语义背景
最终这个 attention 输出 + 位置编码 + FFN → 决定生成哪个 token
- 输出 "Como"
🔍 图中几个关键词总结
图示模块 含义 Qt 当前要预测位置的查询向量 K₁, K₂, …, Kt 之前 token 的 key 向量 V₁, V₂, …, Vt 之前 token 的 value 向量 MatMul(Q, Kᵀ) 点积计算相关性(注意力得分) Mask 防止“看未来” Softmax 转为权重 MatMul(weights, V) 得到上下文加权输出 “Como” 当前输出 token
✅ 总结一句话:
这张图展示了 Transformer 中,如何通过 Attention 机制根据前面 token 的 Key 和 Value,结合当前的 Query 来计算输出,并预测> 下一个词,整个过程是模型“理解上下文、决定下文”的核心。
如果你需要我把这个流程写成代码(如 PyTorch/Hugging Face 的 Attention 实现),或者补充可视化动画,我也可以继续补充!需要吗?
模型规模
后训练的决策
模型的采样
“While most people understand the impact of training on a model’s performance, the impact of sampling is often overlooked. Sampling is how a model chooses an output from all possible options. It is perhaps one of the most underrated concepts in AI. Not only does sampling explain many seemingly baffling AI behaviors, including hal‐ lucinations and inconsistencies, but choosing the right sampling strategy can also sig‐ nificantly boost a model’s performance with relatively little effort. For this reason, sampling is the section that I was the most excited to write about in this chapter.”(摘录原文)
💬 评论:采样是指模型如何从所有可能的选项中选择一个输出。它或许是人工智能领域中最被低估的概念之一。采样不仅能解释许多看似令人困惑的人工智能行为,包括幻觉和不一致性,而且选择正确的采样策略还能以相对较少的努力显著提升模型的性能


No comments to display
No comments to display