AI Compass
Compass

Embeddings

Meaning as direction in space: how words, sentences and images become vectors, what they are used for, and where similarity misleads.

·2 min read·By Fachredaktion Technik
DETAIL
3 sections

The idea

Every word, sentence and image gets a point in a space of hundreds of dimensions. Things that resemble one another sit close together. The question "what is similar?" thereby becomes a distance measurement.

What it is good for

  • Search without exact keywords.
  • Finding duplicate entries.
  • Grouping and sorting texts.
  • The basis of every RAG application.

The kinds

KindWhat is embeddedExample use
Word embeddingA word, independent of sentenceClassical text analysis
Contextual embeddingA token in its sentenceInside a language model
Sentence or passage embeddingA whole text chunkSearch, RAG, grouping
Image embeddingAn imageImage search, duplicate detection
Joint image-text embeddingBoth in one spaceCross-modal search
import numpy as np

def normalise(v):
    # After normalising, the dot product equals cosine similarity.
    return v / np.linalg.norm(v, axis=-1, keepdims=True)

E = normalise(np.random.default_rng(0).normal(size=(5, 384)))
query = normalise(np.random.default_rng(1).normal(size=(384,)))
scores = E @ query
print(np.argsort(scores)[::-1][:3], np.sort(scores)[::-1][:3].round(3))
  • Always normalise before storing or comparing.
  • Use the same prefix the model used in training; many models distinguish query and document embeddings.
  • Store the model version. A model change invalidates the whole index.

How they are trained

Contrastive loss

L = − log( exp(q·d⁺/τ) / ( exp(q·d⁺/τ) + Σⱼ exp(q·d⁻ⱼ/τ) ) )

Similarity to the correct document should be large and to all others small.

q
the query vector
d⁺
the matching document
d⁻ⱼ
non-matching documents from the same batch
τ
temperature, usually 0.02 to 0.07

Quality depends heavily on how hard the counterexamples are. Random ones are too easy: the model learns only to separate topics coarsely. The real lever is hard negatives: documents that look similar and are nevertheless wrong.

Dimension as a trade-off

DimensionMemory per 1 million (float32)Typical quality
3841.5 GBSufficient for most applications
7683.1 GBSlightly better, the usual standard
1,5366.1 GBRarely measurably better
3,07212.3 GBOnly for very large heterogeneous collections

Methods with nested representations allow a long vector to be truncated at an arbitrary point while keeping most of the quality. That enables two-stage search: coarse with 128 dimensions over the whole collection, fine with 768 over the best thousand.

The data protection point

An embedding is not anonymisation. Two attacks are practical:

  • Inversion. A sentence embedding can be approximately inverted to its source text, often almost verbatim for short texts.
  • Membership. Proximity to a known vector reveals whether a particular document is in the collection.

In practice that means the vector index carries the same protection duties as the source collection, including erasure and access requests. See Vector databases.

Related courses and sources

PaperFreeEN

Efficient Estimation of Word Representations

The paper that first established words as vectors with computable meaning. The origin of all embeddings.

The origin of all embeddings; short and still illuminating.

VideoFree180 minEN

Essence of Linear Algebra

Fifteen short films showing what a matrix does to space. If you have only ever seen vectors as lists of numbers, you will see something else afterwards.

For anyone who has only ever seen vectors as lists of numbers and needs an intuition.

3Blue1BrownGo to offer
PaperFreeEN

Learning Transferable Visual Models

Images and text in one shared space. The basis of searching images by description and of image generation.

For understanding image search by description and image generation.

PaperFreeEN

Retrieval-Augmented Generation

The paper that joined retrieval and generation. The origin of the pattern that makes your own documents usable with citations.

For anyone making their own documents usable; the origin of the pattern.

Was this page helpful?
Embeddings