liveBook · Manning

Positional embedding

As explained in Grokking AI Algorithms, Second Edition

Positional encoding is a method of representing an item's position in a sequence with numerical values that can be combined with its content representation.

Positional embedding represents where an item occurs in a sequence and combines that signal with the item’s content representation. This matters because self-attention processes tokens simultaneously: without position information, “The dog bit the man” and “The man bit the dog” look alike. section 11.15

A sinusoidal positional encoding assigns each position a vector of sine and cosine values. Every position receives a distinct pattern, while nearby positions receive similar patterns; the vector width can be adjusted to the model’s embedding width. section 11.15

Combining meaning and location

For a token at position 1, a four-value positional vector can be [0.8415, 0.5403, 0.0100, 1.0000]. Adding it element by element to the token embedding produces one vector containing both what the token means and where it occurs. For example, [-0.008, 0.005, -0.002, 0.009] becomes [0.8335, 0.5453, 0.0080, 1.0090]. section 11.16

A simple implementation follows this sequence:

  • Look up each token’s embedding, add its positional row, and pass the resulting matrix into the Transformer. section 11.16

The same sinusoidal idea can represent a diffusion model’s timestep. Encoding a value such as t = 22 creates a vector that tells the model how much noise is present, so it can adjust denoising strength. section 12.10

Figure 11.29 An odometer analogy showing how sinusoidal positional encoding works
11_29

Use the odometer analogy to see how several changing numerical signals can jointly identify a sequence position. section 11.15

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Works through sinusoidal calculations, vector addition, tables, and NumPy code, while deferring learned downstream interpretation.
Examples
Uses reordered sentences, an odometer analogy, a 32-token batch, four-dimensional vectors, and timestep 22.
What it assumes
Assumes token embeddings and introduces how positional signals supply the missing order information for self-attention.
What you can do after
Apply it via NumPy by generating sinusoidal rows and adding them to token embeddings.
How it connects
Connects positional encoding to token embeddings, self-attention, Transformers, and timestep embeddings.

Also explained in

Compare how 12 books explain positional embedding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.