A dense, multi-dimensional vector (array of numbers) that represents the semantic meaning of data (like words, sentences, or images) in a continuous mathematical space, where similar items are located close to each other.
Imagine a map of a city. Coffee shops are clustered in one area, parks in another, and hospitals in a third. If you want to find a place similar to a specific coffee shop, you just look at what’s nearby on the map.
An embedding is like a GPS coordinate for a piece of data. Instead of latitude and longitude, it uses hundreds or thousands of dimensions. The word “king” might have coordinates that place it very close to “queen” and “royalty,” but far away from “apple” or “car.” By turning text into numbers on a map, computers can understand meaning and similarity.
Embeddings are the bridge between raw, discrete data (like text tokens) and the continuous mathematical operations performed by neural networks.
How they are created:
text-embedding-ada-002, or Sentence Transformers) processes the tokens.Key Properties:
Embeddings are the foundational technology powering modern enterprise search and AI:
A librarian’s mental catalog. Instead of just alphabetizing books by title, the librarian organizes them by theme, tone, and subject matter. A book about “space exploration” is placed physically near books about “astronomy” and “rockets,” making it easy to find related material.
# Generating and comparing text embeddings using Hugging Face
from sentence_transformers import SentenceTransformer, util
# Load a pre-trained embedding model
model = SentenceTransformer('all-MiniLM-L6-v2')
# Sentences to encode
sentences = [
"The cat sat on the mat.",
"A feline is resting on the rug.",
"I love eating pizza for dinner."
]
# Generate embeddings (dense vectors)
embeddings = model.encode(sentences)
# Calculate cosine similarity between the first two sentences
similarity = util.cos_sim(embeddings[0], embeddings[1])
print(f"Similarity between 1 and 2: {similarity.item():.4f}") # High similarity (~0.8+)
# Calculate similarity between 1 and 3
similarity_diff = util.cos_sim(embeddings[0], embeddings[2])
print(f"Similarity between 1 and 3: {similarity_diff.item():.4f}") # Low similarity (~0.1)