Transformer:注意力如何取代 RNN
The Transformer: How Attention Replaced the RNN
Google 的 8 名研究人員提出 Transformer,以並行注意力取代 RNN 和卷積,令模型訓練更快、翻譯表現更佳。基礎模型有 65 million parameters,在英譯德和英譯法任務的 BLEU 分數分別為 28.4 和 41.8,使用 8 GPUs 訓練 3.5 days。
In Part 6, attention let the decoder look back at every word. In June 2017, eight researchers at Google asked a bolder question: what if attention is all you need? They dropped the RNN and the convolution, and built the Transformer. We show the idea in four pictures.
1. Why the RNN had to go
An RNN reads word 1, then word 2, then word 3. Training cannot start step 4 until step 3 is done, so long sentences train slowly. The Transformer reads every word at the same time, so the whole sentence is processed in one parallel pass.
2. Every word looks at every word
Self-attention gives every word a query, a key, and a value. The word it asks about scores every other word, and the scores become weights. Here the word it gives animal a weight of 0.68, because it refers to the animal. The weights in this picture are an example to show the idea.
3. Eight heads, eight views
One attention head sees one pattern, and averaging flattens the rest. The Transformer runs 8 heads in parallel, each working on 64 numbers, then joins them back into one 512-wide vector. Each head can track a different relation, such as a subject and its verb, or a word and the word it refers to.
4. Six layers up, six layers down
The encoder stacks 6 identical layers, each with self-attention and a feed-forward network. The decoder stacks 6 more, adding attention over the encoder output, and masks future words so it cannot peek ahead. Word order comes from a positional encoding, sine and cosine waves added to each word, since attention alone has no sense of order. The base model has 65 million parameters and scored BLEU 28.4 on English to German and 41.8 on English to French, training for 3.5 days on 8 GPUs.
Key takeaway
The Transformer kept the encoder-decoder shape and threw away the recurrence. Attention alone, run in parallel across 8 heads and 6 layers, translated better and trained faster. That one paper became the base of nearly every language model that followed, including the LLMs we use today.
What is Next in This Series
This is the final part of the series. We started with a single neuron in 1958 and ended with the model that runs modern AI. Revisit the journey: Part 1, The Perceptron, Visualized; Part 2, The Multilayer Perceptron: How Backprop Cracked XOR; Part 3, CNNs: How AlexNet Taught Machines to See in 2012; Part 4, RNNs: The Network That Learned to Remember; Part 5, LSTM: The Gates That Taught Networks to Remember; and Part 6, Seq2Seq and Attention: Translation That Looks Back. To see how the Transformer grew into the LLMs of today, read How LLMs Actually Work.
Originally published on LogicStack. Run the code examples right in your browser with our free interactive Python and SQL playgrounds — no setup, no sign-up.
來源:DEV Community · dev.to