llms-from-scratch-cn/Translated_Book/ch03/3.2.ipynb
2024-04-23 16:29:15 +08:00

114 lines
3.8 KiB
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"id": "2c3f5f82",
"metadata": {},
"source": [
"## 3.2 使用注意力机制捕获数据依赖关系"
]
},
{
"cell_type": "markdown",
"id": "fd722cbb",
"metadata": {},
"source": [
"\t\t如前所述在 transformer LLM 之前,通常将 RNN 用于语言建模任务例如语言翻译。RNN 适用于翻译短句,但不适用于较长的文本,因为它们无法直接访问输入中的先前单词。"
]
},
{
"cell_type": "markdown",
"id": "253cd985",
"metadata": {},
"source": [
"\t\t这种方法的一个主要缺点是RNN 在将整个编码输入传递到解码器之前,必须记住处于单个隐藏状态的整个编码输入,如上一节中的图 3.4 所示。"
]
},
{
"cell_type": "markdown",
"id": "c6d08f54",
"metadata": {},
"source": [
"\t\t因此研究人员在 2014 年开发了所谓的 RNN 的 Bahdanau 注意力机制(以相应论文的第一作者命名),该机制修改了编码器-解码器 RNN以便解码器可以在每个解码步骤中选择性地访问输入序列的不同部分如图 3.5 所示。"
]
},
{
"cell_type": "markdown",
"id": "e8421d19",
"metadata": {},
"source": [
"图 3.5 使用注意力机制网络的文本生成解码器部分可以有选择地访问所有输入令牌。这意味着对于生成给定的输出令牌某些输入令牌比其他输入令牌更重要。重要性由所谓的注意力权重决定我们稍后将计算。请注意此图显示了注意力背后的一般思想并未描述Bahdanau机制的确切实现这是本书范围之外的RNN方法。"
]
},
{
"cell_type": "markdown",
"id": "553837ba",
"metadata": {},
"source": [
"![image-20240422132957951](../img/fig-3-5.png)"
]
},
{
"cell_type": "markdown",
"id": "7f381cb8",
"metadata": {},
"source": [
"\t\t有趣的是仅仅三年后研究人员发现RNN架构对于构建用于自然语言处理的深度神经网络并不需要并提出了原始的Transformer架构在第1章中讨论其自注意力机制的灵感来自Bahdanau注意力机制。"
]
},
{
"cell_type": "markdown",
"id": "a986f2a9",
"metadata": {},
"source": [
"\t\t自注意力是一种机制它允许输入序列中的每个位置在计算序列的表示时关注同一序列中的所有位置。自我注意力是基于 transformer 架构的当代 LLM 的关键组成部分,例如 GPT 系列。"
]
},
{
"cell_type": "markdown",
"id": "187245d5",
"metadata": {},
"source": [
"\t\t本章重点介绍如何编码和理解类 GPT 模型中使用的这种自注意力机制,如图 3.6 所示。在下一章中,我们将对 LLM 的其余部分进行编码。"
]
},
{
"cell_type": "markdown",
"id": "30fb7d25",
"metadata": {},
"source": [
"图 3.6 自注意力是变压器中的一种机制,用于计算更有效的输入表示,允许序列中的每个位置与同一序列中的所有其他位置交互并权衡其重要性。在本章中,我们将从头开始编写这种自注意力机制,然后在下一章中对类似 GPT 的 LLM 的其余部分进行编码。"
]
},
{
"cell_type": "markdown",
"id": "9c034d79",
"metadata": {},
"source": [
"![image-20240422133126835](../img/fig-3-6.png)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}