Transformer (machine learning model): Difference between revisions
| Line 21: | Line 21: | ||
===Encoder=== | ===Encoder=== | ||
The receives as input the input embedding added to a positional encoding.<br> | The entire encoder receives as input the input embedding added to a positional encoding.<br> | ||
The encoder is comprised of N=6 | The encoder is comprised of N=6 blocks, each with 2 layers.<br> | ||
Each | Each block contains a multi-headed attention layer followed by a feed-forward layer.<br> | ||
===Decoder=== | ===Decoder=== | ||
Revision as of 15:15, 23 November 2020
Attention is all you need paper
A neural network architecture by Google.
It is currently the best at NLP tasks and has mostly replaced RNNs for these tasks.
Architecture
The Transformer uses an encoder-decoder architecture.
Both the encoder and decoder are comprised of multiple identical layers which have
attention and feedforward sublayers.
Attention
Attention is the main contribution of the transformer architecture.
![]()
The attention block outputs a weighted average of values in a dictionary of key-value pairs.
In the image above:
- \(\displaystyle Q\) represents queries (each query is a vector)
- \(\displaystyle K\) represents keys
- \(\displaystyle V\) represents values
The attention block can be represented as the following equation:
- \(\displaystyle \operatorname{SoftMax}(\frac{QK^T}{\sqrt{d_k}})V\)
Encoder
The entire encoder receives as input the input embedding added to a positional encoding.
The encoder is comprised of N=6 blocks, each with 2 layers.
Each block contains a multi-headed attention layer followed by a feed-forward layer.
Decoder
Code
See Hugging Face
Resources
- Guides and explanations