What changed

Changes since HEAD · 3 pages changed

Legend: added · removed words, the removed ones right before the added ones. A wholly added or removed block is shaded; a whole equation, figure or diagram is framed, the old in red, the new in green. Page titles link to the full preview.

Paper

paper/main.tex

LaTeX edits are highlighted; other paper inputs may not be.

Paperpaper/main.tex
Open the paper PDF

Slides · Attention in sequence models

slides/attention/slides.html
⋯ 4 unchanged slides (1–4)

Slide 5: Attention weights the available context — changed

Changes marked
Clean
Before
After
⋯ 5 unchanged slides (6–10)

A small literature review

lit-review/index.html

A small literature review

This literature review is the editable Quarto source. To get a PDF or LaTeX source, render it with Quarto (see the workspace guide). This review cites the project-root references.bib. The separate paper/main.tex is edited directly and paired with Overleaf, so it keeps its own copy of the entries it cites.

Example claimclaims

Vaswani and colleagues proposed a Transformer architecture based solely on attention mechanisms“We propose a new simple network architecture, the Transformer, based solely on attention mechanisms”— (Vaswani et al. 2017, abstract) (Vaswani et al. 2017). TheBERT later applied the Transformer encoder to bidirectional language representationevidence added“We introduce a new language representation model called BERT, which stands for Bidirectional Encoder Representations from Transformers.”— (Devlin et al. 2019, abstract) (Devlin et al. 2019). Each highlighted claim exposesshows its source quote on hover or keyboard focus.

Background

Recurrent networks read a sequence one token at a time, carrying a hidden state from each token to the next.

Gated cells, such as the LSTM’s, let that state carry information across longer spans, but a signal between two distant tokens still passes through every step between them.

Encoder–decoder models used one recurrent network to read a sentence and a second to write its translation, joined only by the final state of the first.

That final state had to hold the whole sentence, so translations of long sentences lost detail from their beginnings.

Attention was first added to these models to let the decoder look back at every encoder state, rather than at the last one alone.

Convolutional sequence models shortened the path differently, stacking layers so that each widens the span a token can see.

Models compared

Model Year Recurrence Steps between distant tokens
LSTM encoder–decoder 2014 yes one per token between them
RNN with attention 2015 yes one, through attention
Transformer 2017 nonone one
BERT 2019 none one

Encoder–decoder with attention

flowchart LR
  src[Source sentence] --> enc[Recurrent encoder]
  enc --> dec[Recurrent decoder]
  enc -. every state .-> att[Attention]
  att --> dec
  dec --> tgt[Translation]
flowchart LR
  src[Source sentence] --> enc[Recurrent encoder]
  enc --> dec[Recurrent decoder]
  enc -. every state .-> att[Attention weights]
  att --> dec
  dec -- query --> att
  dec --> tgt[Translation]

Attention in one line

Each token’s output is a weighted average of every token’s value:value, its weights scaled by the key size so the softmax stays smooth:

\[ \operatorname{Attention}(Q,K,V) = \operatorname{softmax}\left(QK^T\right)V \]

\[ \operatorname{Attention}(Q,K,V) = \operatorname{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V \]

Reading the equation

Q, K and V arehold the queries, keys and values, one row per token.token, and \(d_k\) is the key size.

scores = q @ k.T / np.sqrt(k.shape[-1])
weights = softmax(scores, axis=-1)
out = weights @ v

How far a signal travels

path t1 t1 t2 t2 t1->t2 step t4 t4 t1->t4 attention t3 t3 t2->t3 step t3->t4 step path t1 t1 t2 t2 t1->t2 step t4 t4 t1->t4 attention, one step t3 t3 t2->t3 step t3->t4 step

The longest path a signal takes between two tokens of a sequence, by layer type. The convolution has kernel 5, stride 1 and no dilation, so each layer widens a token’s view by 4 and its path is the number of layers needed to span the sequence.

The tokens one position can see after each layer of that convolution, in a 512-token2,048-token sequence.

Takeaways

  • Recurrence reads one token at a time, so the tokens of a sequence can’t be processed in parallel during training.
  • Gating eases, but doesn’t remove, the long path between distant tokens.
  • A fixed-size final state limits how much of a long input sentence a decoder can draw on.
  • Convolutions shorten the path, but only by stacking more layers as the span grows.
  • Attention connects every pair of tokens in one step.step, however far apart they are.
  • The Transformer drops recurrence entirely.
  • BERT pre-trains the Transformer’s encoder alone, reading in both directions.

References

Devlin, Jacob, Ming-Wei Chang, Kenton Lee, and Kristina Toutanova. 2019. “BERT: Pre-Training of Deep Bidirectional Transformers for Language Understanding.” https://arxiv.org/abs/1810.04805.
Vaswani, Ashish, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Lukasz Kaiser, and Illia Polosukhin. 2017. “Attention Is All You Need.” https://arxiv.org/abs/1706.03762.