👀 自注意力:每个词看全场👀 Self-Attention: Every Word Scans the Room
上一片,残差连接给网络搭好了直通桥。这一片推门进入大模型:机器人读句子时,词和词之间怎么互相通气?Last leaf, residual connections built the network a straight bridge. This leaf pushes open the door to large models: when the robot reads a sentence, how do its words pass notes to each other?
第 1 步:不用排队,一眼看全场Step 1: No queue — see the whole room at once
先看看老一代读句子的网络,为什么慢。First, why the previous generation of sentence-reading nets was slow.
老办法叫循环神经网络(RNN,Recurrent Neural Network):记忆像接力棒,一个字一个字往下传,传到句尾,开头早已模糊。自注意力换了个打法:每个词同时向全场广播「我重要吗」,一步就把所有词看个遍。不用排队,也就没有遗忘。The old way is the recurrent neural network (RNN): memory travels like a relay baton, word by word — by the end of the sentence, the start is already blurry. Self-attention plays it differently: every word shouts “Do I matter?” to the whole room and sees every other word in one step. No queue, no forgetting.
每个词「看全场」时,到底在看什么?When a word scans the room, what exactly is it looking for?
第 2 步:三个角色 Q、K、VStep 2: Three roles — Q, K, V
每个词手里有三样道具:查询 Q(Query,我想找什么)、键 K(Key,我是什么)、值 V(Value,我的内容)。比如「打盹」的 Q 是「谁在打盹」,而「小猫」的 K 回答「我会打盹」——对上了!匹配分越高,从对方的 V 里拿走的内容就越多。查一查、对一对、拿内容,就这么简单。Every word holds three props: a query Q (what I am looking for), a key K (what I am), and a value V (what I carry). The query of “naps” asks “who naps?”, and the key of “kitten” answers “I do” — a match! The higher the match score, the more of that word's value you take away. Query, match, take — that is the whole trick.
这对长句子有多重要?看一个经典例子。Why does this matter so much for long sentences? Here's a classic example.
第 3 步:隔多远,都直接连Step 3: Any distance, one direct link
「小猫抓不到老鼠,因为它太胖了。」这里的「它」是谁?自注意力让「它」直接问遍全句:和「小猫」的匹配分最高(胖,才会抓不到),一步拿到「小猫」的内容。中间隔了多少个词都一样远。RNN 顺着传记忆,句子一长就够不着;自注意力,隔多远都一步直达。“The cat can't catch the mice because it is too fat.” Who is “it”? Self-attention lets “it” question the whole sentence: the top match is “cat” (being fat is why the catching fails), and it grabs the cat's content in one step. The number of words in between does not matter. An RNN loses reach as the sentence grows; self-attention reaches across any distance.
🎮 你来当自注意力(30 秒)🎮 Your turn: think like self-attention (30 seconds)
道理讲完了。下面 4 个句子,每个都有一个目标词——帮它点出「最该看」的那个词,看连线的火花飞起来。Theory done. Below are 4 sentences, each with a target word — tap the word it should look at, and watch the link light up.
一句话记住它:自注意力 = 每个词一眼看全场:用 Q 去找、拿 K 来对、按匹配分取 V,多远都一步直达。Remember it in one line: self-attention = every word scans the whole room: query asks, key answers, value gets taken — any distance, one direct link.
老办法(RNN)顺着传记忆,句子一长就忘;自注意力同时看全场The old way (RNN) passes memory along and forgets; self-attention sees the whole sentence at once三个角色:Q 想找什么、K 我是什么、V 我有什么Three roles: Q asks what I want, K says what I am, V holds my content「它」能一步连到远处的「小猫」——距离不是问题“it” links straight to a faraway “cat” — distance is no problem
内容参考 OpenStax 等公开教材,多来源核对 · AI 生成、人工审核 · 发现错误欢迎指正,帮这片叶子长得更好。Based on OpenStax and other open textbooks, cross-checked across sources · AI-generated, human-reviewed · Spotted a mistake? Tell us — help this leaf grow.