Skip to content
Module 04 of 1255 min readBeginner

NLP foundations — tokens, embeddings, attention

How language becomes numbers: tokenisation, word embeddings, the attention mechanism that powers every modern LLM.

33%

Listen along

Read “NLP foundations — tokens, embeddings, attention” aloud

Plays in your browser using on-device text-to-speech — nothing leaves the page.

Learning objectives

By the end of this module, you should be able to:

  • 01Explain how text becomes numbers via tokenisation
  • 02Understand word embeddings as positions in a high-dimensional vector space where semantic similarity = geometric similarity
  • 03Articulate what the attention mechanism does: weight every input token by relevance to every other
  • 04Read 'Attention Is All You Need' and follow the architecture diagram

Computers don't read text; they multiply numbers. NLP is mostly about how to turn text into useful numbers and back.

Tokenisation

First, text is broken into tokens — units roughly the size of common sub-words. 'unemployment' might be one token; 'photosynthesise' might be three. Modern tokenisers (Byte-Pair Encoding, SentencePiece) learn the optimal vocabulary from data. A typical English page is ~500-700 tokens.

Embeddings

Each token gets mapped to a vector — a list of, say, 1024 numbers. Tokens with similar meaning end up at nearby positions in vector space. 'King − Man + Woman ≈ Queen' is the famous Word2Vec demo of how semantic relations are encoded as geometric relations. Modern LLMs learn embeddings as part of training; you don't separately compute them.

Attention

The transformer's central innovation. For each token in the sequence, attention asks: how relevant is every other token to this one? It computes a weighted average of all other tokens' representations, weighted by relevance. This lets the model decide, at every layer, which parts of the context matter for the current token.

text
Sentence: "The bank raised rates."
When processing "bank", attention can weight "rates" highly
— enough to interpret "bank" as a financial institution
rather than a river bank.

Multi-head attention

Instead of one attention computation, the transformer runs many in parallel (typically 8-128 'heads'). Each head can specialise in a different kind of relationship — syntactic dependencies, semantic similarity, positional patterns. The outputs are concatenated and projected back to the embedding dimension.

The full transformer block

Multi-head self-attention → add residual → layer norm → feedforward network → add residual → layer norm. Stack 12, 24, 96 of these blocks, train on trillions of tokens, and you have a modern LLM.

If you read one paper, read this one

'Attention Is All You Need' is eight pages. The mathematics is elementary. The clarity of presentation is extraordinary. Read it once carefully; it's the founding document of the entire LLM era.

Exercise

An analyst pastes a 2,000-word central-bank monetary-policy statement into a Claude or ChatGPT API call and asks for a summary. (1) Roughly how many tokens does this consume on input? (2) If the model's context window is 200,000 tokens, what proportion of capacity does this use? (3) Why might the analyst care about tokens beyond cost — what other constraint does token count drive? (4) Briefly: explain in one sentence why we tokenise text at the sub-word level (BPE / SentencePiece) rather than character-by-character or word-by-word.

Key takeaways

  • Tokenisation breaks text into sub-word units; a typical English word is 1-2 tokens
  • Embeddings turn each token into a vector (typically 768 or 1024 or 4096 dimensions in modern models)
  • Self-attention asks: for each token, how relevant is every other token to me right now? The model uses that to weight information flow
  • The transformer is alternating layers of (multi-head self-attention) and (feedforward network), with residual connections and layer norm

Further reading

  1. 01

    Attention Is All You Need

    Vaswani et al. · NeurIPS · 2017

  2. 02

    The Illustrated Transformer

    Jay Alammar · 2018The clearest visual explanation of how transformers work.

  3. 03

    Speech and Language Processing (3rd Edition Draft)

    Dan Jurafsky & James H. Martin · Stanford University

Loading progress…
LeadAfrikPublic Economics Hub