Artificial Intelligence · Foundations
Understanding Transformers
Self-attention and why it replaced recurrence.
1 min readPublished Updated
- transformers
- attention
Transformers replace recurrence with self-attention, letting every token look at every other token.
ts
type AttentionScores = number[][];
function softmax(row: number[]): number[] {
const max = Math.max(...row);
const exps = row.map((v) => Math.exp(v - max));
const sum = exps.reduce((a, b) => a + b, 0);
return exps.map((v) => v / sum);
}| Component | Purpose |
|---|---|
| Query/Key/Value | Projects tokens into comparable spaces |
| Multi-head attention | Lets the model attend to several relationships at once |
| Feed-forward block | Adds per-token nonlinearity |
Anatomy of a block#
From text to tokens#
Before attention can run, text is cut into tokens. Edit the sentence below and watch how byte-pair encoding groups characters.
Tokenizer playground