█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/LLM Research and NLP/Transformers and NLP Foundations/Embeddings and Positional Encoding
45 minAdvanced

Embeddings and Positional Encoding

After this lesson, you will be able to: Explain how token ids become vectors (embeddings) and how transformers inject word order through positional encoding (sinusoidal, learned, and RoPE).

Tokens are integers; a transformer needs vectors, and it needs to know order because attention is order-blind by default. This lesson covers embeddings and the positional encoding schemes you will see named in every architecture paper.

Prerequisites:Training a Tokenizer

Embeddings: ids to vectors

An embedding layer is a lookup table: a matrix of shape (vocab_size, d_model). Token id 4217 just selects row 4217, a learned d_model-dimensional vector. These vectors are trained, so semantically related tokens drift toward similar directions. This is the modern descendant of word2vec and GloVe, with one key difference: in a transformer the embedding is only the starting point; attention layers then make each token's representation context-dependent, which is why the same word gets different vectors in different sentences (contextual embeddings).

ℹ️ Attention is permutation-invariant, so order must be added

Self-attention computes relationships between tokens as a set: shuffle the inputs and (without position info) you get the same outputs shuffled. But 'dog bites man' and 'man bites dog' must differ. So transformers add positional information to the token embeddings. How they do it is a real architectural choice with measurable effects on length generalization, and it is a frequent topic in efficiency and long-context papers.

Three positional schemes you will meet in papers

Sinusoidal (original Transformer): fixed sine/cosine functions of position added to embeddings; no parameters, extrapolates somewhat to longer sequences. Learned absolute (BERT, GPT-2): a trainable position embedding per slot up to a max length; simple but cannot exceed the trained length. Rotary Position Embedding (RoPE, used by Llama and most modern LLMs): rotates the query/key vectors by an angle proportional to position, encoding *relative* position inside attention; it generalizes better to longer contexts and underlies context-extension tricks (position interpolation, YaRN). ALiBi is another relative scheme (a linear bias on attention scores). When a paper says 'we use RoPE with a base of 10000,' you now know what that means.

Common mistakes only experienced researchers catch

Assuming a model can handle inputs longer than its trained positional range without degradation (learned absolute positions simply have no embedding past the limit). Confusing the embedding dimension d_model with the vocabulary size. Forgetting that tying the input embedding and output projection (weight tying) is a real, common choice that saves parameters. Treating positional encoding as a solved detail when it is precisely where many long-context papers make their contribution.

Quick Check

Why do transformers need positional encoding at all?

Pick the correct reason.

Sign in and purchase access to unlock this lesson.

Sign in to purchase
←Training a Tokenizer: BPE, WordPiece, SentencePiece
Back to Transformers and NLP Foundations
Attention from the Math Up→