liveBook · Manning

Contextualized representation

As explained in Transformers in Action

A contextual embedding is a vector representation whose value reflects the surrounding context of the represented text element.

A contextualized representation is a vector whose value depends on the text around the element it represents. A static word embedding assigns one vector to “bank” whether the sentence concerns money or a river. A transformer instead produces different token vectors for those two uses. section 3.4.1

Encoder-only models build these representations using bidirectional attention: a token can attend to words on either side. During masked language modeling, the model uses that context to predict masked tokens. The masked-token prediction head is used for pretraining and removed for fine-tuning; the contextual representations remain useful for downstream tasks. section 3.3.1

The representation’s scale determines how it is used:

  • Token vectors distinguish a word’s meaning across contexts, such as the financial and riverside uses of “bank.” section 3.4.1
  • Sentence and document vectors can be compared with measures such as cosine similarity for semantic search and retrieval. section 3.4.1
Listing 3.6 Generating sentence embeddings using a pretrained transformer
from sentence_transformers import SentenceTransformer

model = SentenceTransformer('Owen/Owen3-Embedding-0.6B')   #1

sentences = [   #2
    'Transformers capture context effectively', 
    'Embeddings help understand context'
]

embeddings = model.encode(sentences)  #3

print(embeddings.shape)  # output: (2, 1024)   #4

The `SentenceTransformer` example encodes two sentences and prints a `(2, 1024)` result: one vector per input sentence. It demonstrates obtaining representations without implementing the transformer. section 3.4.1

Listing 3.7 Similarity search between documents and query
model = SentenceTransformer('Owen/Owen3-Embedding-0.6B')   #1

queries = [  #2
    'What is an embedding in machine learning?',
    'How are embeddings generated in transformer models?'
]

documents = [
    'Embeddings are vector representations of data, such as words or 
    sentences, used to capture semantic relationships in machine learning 
    models.',
    'Transformer models generate embeddings by passing input tokens through 
    multiple layers of attention and feedforward networks, producing 
    contextualized vector outputs.'
]

query_embeddings = model.encode(queries, prompt_name='query')
document_embeddings = model.encode(documents)

similarity_scores = util.cos_sim(
query_embeddings, document_embeddings)  #3

print(similarity_scores)

The search example encodes queries and documents separately, then applies cosine similarity to their vectors. The resulting scores provide a basis for comparing which documents match each query. section 3.4.1

How Transformers in Action covers it

How deep it goes
Defines the contrast with static vectors and explains bidirectional pretraining, but stops short of deriving layer-level vector computations.
Examples
Uses contrasting meanings of “bank” and code that encodes sentences, queries, and documents for similarity comparison.
What it assumes
Introduces vectors and static embeddings before contextual ones; executable examples use Python and a pretrained model.
What you can do after
Apply it via SentenceTransformer to encode text and compare query–document vectors with cosine similarity.
How it connects
Connects static word embeddings, bidirectional attention, masked language modeling, cosine similarity, and semantic retrieval.

Also explained in

Compare how 2 books explain contextualized representation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Transformers in Action. The section links point to the parts of the book it draws on.