Contextual Embeddings

LLMs interview question · Medium difficulty

How does a token's initial embedding become a contextual representation in an LLM?medium

Status
✓
Type
conceptual
Topic
contextual-embeddings
Frequency
common
Tags
embeddings, self-attention, contextual-representation, polysemy
Answer

Text is tokenized, each token ID is mapped to a fixed initial embedding vector, and that embedding is only the starting point — once it passes through the transformer's self-attention layers, it becomes a contextual representation that shifts based on surrounding words.

Explanation

The input flow is text → tokens → token IDs → embedding vectors → transformer layers. Tokens that appear in similar contexts end up with similar initial vectors (doctor ≈ physician, car ≈ vehicle), but that initial embedding is the same regardless of sentence. The classic example: "I deposited money in the bank" and "I sat near the river bank" start with the identical initial embedding for "bank," but after self-attention pulls in context from the surrounding words ("deposited," "money" versus "sat," "river"), the resulting representations diverge to reflect the two different meanings. This is the core reason transformer-based models handle polysemy (words with multiple meanings) far better than static embedding methods like Word2Vec, which assign one fixed vector per word regardless of context.

Follow-upWhy would a static embedding method like Word2Vec struggle with a word like 'bank'?
Follow-upAt which point in the architecture does 'bank' start to differentiate between its two meanings?
Follow-upIs the final contextual representation of a token still called an 'embedding'?