liveBook · Manning

Embedding

As explained in Transformers in Action

A fixed-length vector is an ordered numerical representation with a predetermined number of components.

An embedding is a numerical vector representing a token, sentence, document, image, or other input. Its dimensions provide a space in which related inputs can be compared: semantically similar items tend to lie near one another. An embedding model produces a fixed-length vector for an input sequence, making inputs of different lengths usable in retrieval, ranking, and clustering. section 3.4.1 section 3.4

Token embeddings and whole-input embeddings serve different jobs. A learned embedding layer maps each text token ID to a vector; positional information is combined with those vectors before attention. Older methods such as Word2Vec and GloVe assign one vector per word, so “bank” has the same representation in financial and riverside contexts. Transformer-based contextual embeddings instead change with surrounding text. Encoder-only models use bidirectional attention to produce representations suited to understanding and retrieval rather than autoregressive generation. section 2.2.3 section 3.4.1 section 3.3

Encode and compare sentences

With SentenceTransformer('Owen/Owen3-Embedding-0.6B'), model.encode(['Transformers capture context effectively', 'Embeddings help understand context']) produces an array of shape (2, 1024): two fixed-length vectors. For search, encode queries and documents separately, then pass their vectors to util.cos_sim to obtain a query-by-document similarity matrix. The illustrated two-query, two-document result scores one document higher for each query; these scores support ranking, not generated answers. section 3.4.1

In retrieval-augmented generation, documents are embedded and stored in a searchable vector database. A query is encoded with the same model; vector similarity selects relevant documents, which are added to the language model’s prompt. This gives the generator access to external, potentially newer or domain-specific information without retraining it. section 3.4

Embeddings also connect modalities. Text starts with subword token IDs; image encoders can split an image into patches, flatten each patch, and learn a projection into the transformer’s embedding dimension. Positional embeddings retain spatial arrangement. Modality-specific encoders can then project text, image, or audio features into a shared space for cross-modal comparison. This learned alignment is more than concatenating features. section 6.3 section 6.3.1 section 6.2

Figure 6.4 Linear projection W maps patch vectors from an input space ℝdto an embedding space ℝk. Dashed arrows indicate the mapping of example patches through W.
figure

The patch-projection diagram shows how a learned mapping turns raw image-patch vectors into vectors a transformer can process. section 6.3.1

How Transformers in Action covers it

How deep it goes
Defines vector representations, contrasts static and contextual embeddings, and demonstrates encoding and similarity; does not derive embedding-model training.
Examples
Uses ambiguous “bank” contexts, a two-sentence embedding array, query–document similarity scores, and projected image patches.
What it assumes
Introduces embeddings from vector intuition while assuming familiarity with basic tokens and transformer components for the attention workflow.
What you can do after
Apply it via SentenceTransformer to encode sentences and compare query and document vectors with cosine similarity.
How it connects
Connects tokenization and positional embeddings to attention, and vector similarity to retrieval-augmented generation.

Also explained in

Compare how 47 books explain embedding →

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.