An embedding is just a list of numbers — a vector — that a model produces to represent something: an image, a sentence, a song clip, a model’s own output. The number itself means nothing on its own. What matters is distance: two things the model considers similar end up with vectors that sit close together in that numeric space, and two unrelated things end up far apart. That property is what makes embeddings useful for search, deduplication, and clustering — and it’s a foundational tool for anyone doing creative or research work with large datasets.
Why this matters more than keyword search
A keyword search for “sunset over water” only finds items whose text literally contains those words. An embedding-based search finds items that are about a sunset over water, including an image with no caption at all, because the model that generated the embedding learned the visual concept directly — not the words describing it. That’s the whole reason tools like reverse image search, “find more like this,” and semantic search over a research paper archive all work the way they do.
Generating your first embeddings
For images, CLIP (OpenAI’s contrastive language-image model, widely available through Hugging Face) is the standard starting point because it embeds images and text into the same vector space — meaning you can search images using a text description, or vice versa:
from transformers import CLIPModel, CLIPProcessor
from PIL import Image
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
image = Image.open("photo.jpg")
inputs = processor(images=image, return_tensors="pt")
embedding = model.get_image_features(**inputs)
For text alone, Sentence Transformers is the simplest library — one line turns a sentence into a vector:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("all-MiniLM-L6-v2")
embedding = model.encode("a photo of a sunset over calm water")
Both return a fixed-length vector (512 numbers for this CLIP model, 384 for this Sentence Transformers model) — the exact length depends on the model, not on the input’s length.
Comparing vectors: cosine similarity, not Euclidean distance
Once you have embeddings, “how similar are these two things” is almost always measured with cosine similarity — the angle between two vectors, not the straight-line distance between them. This matters because embedding magnitude often doesn’t carry meaning on its own, while direction does:
import numpy as np
def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
score = cosine_similarity(embedding_1, embedding_2)
The result ranges from -1 (opposite) to 1 (identical direction), with values above roughly 0.8 usually indicating genuinely similar content for most models — though the exact threshold that “feels right” varies by model and dataset, and is worth calibrating against your own examples rather than trusting a number from someone else’s project.
Searching more than a handful of items
Comparing one query against a few dozen embeddings with a loop is fine. Comparing it against tens of thousands or more needs an actual vector index — a data structure built specifically for fast nearest-neighbor lookup. FAISS (Meta’s open-source library) is the standard starting point for a local, in-process index; it trades a small amount of accuracy for a large speed gain once a dataset gets big, which is the right trade for almost any exploratory or creative-research use case.