Fun with Learning Technology
LearnCoursesQuestionsTracksToolsNewsExplorePractice
Fun with Learning Technology

A new problem, explained clearly, every day.

Subscribe
Learn
  • Lessons
  • Topics
  • News
  • Tools
  • Courses
  • Career tracks
  • Everything
Site
  • About
  • Contact
  • Support
  • Privacy
  • Terms
Get the daily one

One email per new problem. No spam.

Request a tutorial

Requests shape what gets made next.

© 2026 Fun with Learning TechnologyRSS
Home›Courses›Generative AI›What are Embeddings

Embeddings and Semantic Search

What are Embeddings

Embeddings are numerical representations of data that map complex inputs into high-dimensional vector spaces. They enable semantic understanding by positioning similar concepts close to each other, allowing mathematical operations to determine relationship strength. You should use them whenever you need to perform similarity searches, clustering, or context retrieval for machine learning models.

The Core Concept of Vector Representation

At the fundamental level, computers do not understand words; they understand numbers. To bridge this gap, we transform textual data into embeddings, which are dense vectors of floating-point numbers. Think of these numbers as coordinates in a multi-dimensional space, where each dimension represents a specific latent feature learned during training. By mapping inputs to this space, the semantic essence of a document becomes a geometric property. When two words appear in similar contexts during training, their resulting vectors are pulled toward the same region of the space. This transformation allows algorithms to treat linguistic similarity as a geometric distance calculation rather than just string matching. Because this is a continuous vector space, you can perform arithmetic on meanings, such as finding the mathematical middle ground between two abstract concepts, which provides the foundational logic for all modern retrieval-augmented generation systems.

# Calculate the dimensionality of a sample embedding vector
import numpy as np

# A hypothetical 4-dimensional embedding for the word 'king'
embedding = np.array([0.12, -0.45, 0.88, 0.02])

# The number of elements corresponds to the model's latent dimensions
print(f"Embedding dimension: {len(embedding)}")

Measuring Semantic Closeness

Once your data is represented as vectors, the question of similarity becomes a matter of spatial orientation. If you have two vectors, the most effective way to determine how related their meanings are is through Cosine Similarity. Unlike Euclidean distance, which measures the straight-line gap between points, cosine similarity measures the cosine of the angle between two vectors. This is critical because it normalizes for the length of the documents; two vectors pointing in the same direction represent the same semantic meaning regardless of how much text the documents contain. By calculating the dot product of normalized vectors, you derive a score between -1 and 1, where 1 indicates perfect alignment. This mathematical operation is the engine that drives semantic search, allowing a system to retrieve content that is conceptually identical to the user query even if the search terms do not overlap at all.

def cosine_similarity(a, b):
    # Calculate the dot product and magnitudes to find the cosine angle
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

vec_a = np.array([0.5, 0.1, 0.9])
vec_b = np.array([0.4, 0.2, 0.8])
# High similarity score implies closely related meanings
print(f"Similarity: {cosine_similarity(vec_a, vec_b):.4f}")

Building the Vector Database

To perform efficient lookups across millions of data points, you need more than just a list of arrays; you need a specialized index. A vector database stores these embeddings alongside metadata to enable rapid approximate nearest neighbor search. Instead of exhaustively comparing a query vector against every single document in your database—which would be prohibitively expensive at scale—the database organizes vectors using structures like Hierarchical Navigable Small Worlds (HNSW). These graphs connect similar items into clusters, allowing the search algorithm to jump between neighbors until it converges on the most semantically relevant items. This process essentially narrows down the search field exponentially, turning a massive linear scanning problem into a logarithmic navigation task. By caching these representations in a structured index, you ensure that the system can retrieve the most contextually relevant information in milliseconds, which is vital for real-time generative responses.

# Simulate a small vector index search
vectors = np.array([[0.1, 0.2], [0.9, 0.8], [0.15, 0.25]])
query = np.array([0.12, 0.22])

# Find the index of the closest vector by computing minimal distance
distances = [np.linalg.norm(v - query) for v in vectors]
print(f"Closest index: {np.argmin(distances)}")

Contextual Retrieval in Generation

The power of embeddings truly shines when integrated into a retrieval loop for generative models. When a user asks a complex question, the system first converts the question into an embedding. This query vector is used to search the vector database for the most relevant "chunks" of information. These retrieved chunks serve as the factual context that the model reads before synthesizing its response. This approach solves two primary problems: it reduces the chance of the model hallucinating by grounding it in verifiable data, and it allows your knowledge base to be updated without needing to retrain the underlying model. By treating information as a set of points in a vector space, you create a system that can reason over your own proprietary or real-time data, effectively augmenting its internal intelligence with specific, curated knowledge that it otherwise could never access or store within its static parameters.

# Function to find the most relevant context snippet
def retrieve_context(query_vec, database_vecs, snippets):
    scores = [cosine_similarity(query_vec, v) for v in database_vecs]
    # Return snippet with the highest relevance score
    return snippets[np.argmax(scores)]

knowledge_base = ["Embeddings are vectors.", "Generative AI uses tokens."]
# Mock retrieval logic
print(retrieve_context(np.array([0.1, 0.9]), [np.array([0.1, 0.8]), np.array([0.9, 0.1])], knowledge_base))

Dimensionality and Model Sensitivity

The dimensionality of an embedding model determines the granularity of the semantic space. Lower-dimensional embeddings are computationally cheap but may conflate different concepts as similar, leading to noisy retrieval results. Higher-dimensional embeddings offer more "room" to separate nuances and complex relational structures, but they increase the memory footprint and the computational cost of every search operation. Choosing the right embedding model is a balancing act between the complexity of your domain vocabulary and your infrastructure latency requirements. Furthermore, it is essential to keep the embedding model consistent: if you index your data using one specific model, you must use that exact same model to process any future user queries. If you switch models, the geometric alignment of your data breaks, rendering your entire index obsolete because the new vectors will inhabit a different coordinate system entirely.

# Demonstrating that dimension mismatch causes failure
vec_a = np.random.rand(768) # Typical model dimension
vec_b = np.random.rand(1536) # Different model dimension

try:
    # This operation will fail due to incompatible dimensions
    print(np.dot(vec_a, vec_b))
except ValueError as e:
    print(f"Dimension mismatch error: {e}")

Key points

  • Embeddings convert textual data into high-dimensional numerical vectors.
  • Geometric distance in vector space correlates directly to semantic similarity.
  • Cosine similarity is the preferred metric for evaluating vector closeness.
  • Vector databases use efficient indexing to perform fast similarity searches.
  • Embeddings enable models to access external data via retrieval-augmented generation.
  • Dimensionality selection impacts both the accuracy and the computational cost of the system.
  • Consistency between the indexing model and the query model is strictly required.
  • Embeddings allow computers to perform logical operations on language through spatial relationships.

Common mistakes

  • Mistake: Thinking embeddings are just compression algorithms. Why it's wrong: Compression aims to reconstruct data, whereas embeddings aim to preserve semantic relationships for downstream tasks. Fix: View embeddings as a learned representation that maps high-dimensional data into a lower-dimensional space where closeness implies similarity.
  • Mistake: Assuming that larger embedding dimensions always yield better performance. Why it's wrong: Excessively high dimensions can lead to overfitting and the 'curse of dimensionality,' where distance metrics become less meaningful. Fix: Optimize dimension size based on the complexity of the dataset and the specific model architecture.
  • Mistake: Treating embeddings as static entities regardless of the context. Why it's wrong: Modern transformer-based embeddings are contextual, meaning the representation changes based on surrounding input. Fix: Recognize that a word's vector representation is a function of its entire sequence, not a static lookup table entry.
  • Mistake: Equating vector distance with physical similarity. Why it's wrong: Similarity in embedding space represents semantic or functional relationship, which may not align with human intuition of 'looking' similar. Fix: Focus on how the model is trained; if trained on user behavior, distance represents user preference, not visual likeness.
  • Mistake: Neglecting to normalize embedding vectors before similarity calculation. Why it's wrong: Dot product similarity is highly dependent on vector magnitude; without normalization, the results are skewed by the frequency of terms. Fix: Always apply L2 normalization to ensure that comparisons focus on directionality rather than magnitude.

Interview questions

Can you explain in simple terms what an embedding is in the context of Generative AI?

An embedding is essentially a way to represent complex data, like words, images, or documents, as a list of numbers called a vector. In Generative AI, we map these inputs into a multi-dimensional space where the distance between vectors represents their semantic meaning. If two concepts are closely related, their embedding vectors will be positioned very close to each other in this space, allowing models to understand relationships without needing explicit rules.

Why do we use embeddings instead of just using raw text or raw pixel data for modern AI models?

Raw data, such as a string of text, is sparse and lacks inherent meaning for mathematical operations. Embeddings solve this by capturing the context and intent of the data through dense, low-dimensional representations. If we used raw text, the model wouldn't know that 'cat' and 'feline' are similar. By converting them into embeddings, we provide the model with a continuous numerical space where it can calculate similarity using functions like cosine similarity or Euclidean distance, which is fundamental for high-performance reasoning.

How does the training process actually create meaningful embedding vectors?

Embeddings are typically generated during the pre-training phase of a neural network. The model is trained on a massive corpus using objectives like predicting missing words in a sentence. As the model learns, it adjusts the weights of the embedding layer so that words that appear in similar contexts receive similar vector representations. For example, in a small embedding space, the vector for 'king' might be calculated as: vector('king') = vector('man') - vector('woman') + vector('queen'), demonstrating that the training captures underlying structural patterns.

Could you compare sparse vector representations like TF-IDF with dense vector embeddings?

Sparse representations like TF-IDF rely on word counts and are high-dimensional, meaning each word has its own dimension, leading to massive, mostly empty vectors. They are useful for keyword matching but fail to capture semantic context. In contrast, dense embeddings compress this information into a fixed-size vector of, say, 768 dimensions. Dense embeddings capture latent meaning and synonyms, which is superior for Generative AI tasks like semantic search, where you need to retrieve relevant context even if the exact keywords do not match.

What role do embeddings play in the context of Retrieval-Augmented Generation (RAG)?

Embeddings are the backbone of RAG. When a user asks a question, we first convert that query into an embedding vector. We then perform a vector similarity search against a database containing embedded chunks of our knowledge base. We retrieve the most semantically similar chunks and feed them into the large language model as context. This allows the model to generate accurate, source-grounded answers without needing to be fine-tuned, as it relies on the retrieval of relevant information rather than just its internal memory.

How would you handle the challenge of domain-specific vocabulary when working with pre-trained embeddings?

Pre-trained embeddings are often trained on general web data, which might fail on niche terminology like specific medical or legal jargon. To handle this, we can perform domain-adaptive pre-training or fine-tuning, where we continue training the embedding model on a specialized corpus. Alternatively, we can use techniques like adding a linear projection layer or incorporating metadata. Code-wise, using a library like SentenceTransformers, you would load a base model, define a DataLoader with your custom dataset, and use a ContrastiveLoss function to ensure domain-specific synonyms are pulled closer together in the vector space.

All Generative AI interview questions →

Check yourself

1. When comparing two text inputs using cosine similarity on their embeddings, what is the primary goal of the comparison?

  • A.To measure the number of overlapping keywords between the two texts.
  • B.To determine the degree of semantic alignment between the underlying concepts.
  • C.To calculate the computational cost required to generate the model output.
  • D.To ensure that the character count of both inputs is statistically similar.
Show answer

B. To determine the degree of semantic alignment between the underlying concepts.
Cosine similarity measures the cosine of the angle between vectors, which identifies semantic alignment. Keyword overlap (option 0) is a surface-level feature, not a function of embeddings. Computational cost (option 2) is independent of similarity, and character count (option 3) is unrelated to vector semantics.

2. Why is it necessary to map discrete inputs like words into a continuous vector space in Generative AI?

  • A.To allow the model to perform mathematical operations to identify complex patterns.
  • B.To reduce the memory required to store the dictionary of valid inputs.
  • C.To ensure that the model output is always limited to a predefined set of choices.
  • D.To bypass the need for training the model on large datasets.
Show answer

A. To allow the model to perform mathematical operations to identify complex patterns.
Continuous vector spaces allow models to use calculus and linear algebra to learn relationships. Memory reduction (option 1) is not the goal of embedding. Limiting output (option 2) contradicts the purpose of Generative AI. Training (option 3) is a prerequisite for creating meaningful embeddings.

3. If two terms are frequently found in the same structural context in a massive training corpus, how will their embeddings likely appear?

  • A.They will be orthogonal to each other in the vector space.
  • B.They will represent the exact same point in the vector space.
  • C.They will be positioned closer together in the vector space.
  • D.They will have no relationship in the vector space.
Show answer

C. They will be positioned closer together in the vector space.
Embeddings are trained to cluster contextually similar items together; therefore, similar contexts yield proximity. Orthogonal vectors (option 0) imply no similarity. The same point (option 1) is unlikely due to nuance. No relationship (option 3) ignores the fundamental mechanism of embedding training.

4. What is the primary advantage of using contextualized embeddings over static embeddings?

  • A.They require significantly less GPU memory during inference.
  • B.They represent the meaning of a word by considering the surrounding sequence.
  • C.They are easier to compute because they do not require a neural network.
  • D.They are always exactly the same length regardless of the input.
Show answer

B. They represent the meaning of a word by considering the surrounding sequence.
Contextual embeddings account for polysemy (words with multiple meanings) based on the sequence. GPU memory (option 0) is actually higher for complex models. Neural networks (option 2) are required to create them. Fixed length (option 3) is a feature of most embedding models, not a distinction for contextual ones.

5. What occurs when an embedding space is poorly optimized or the data is insufficiently diverse?

  • A.The model produces embeddings that are all identical.
  • B.The vectors fail to capture meaningful relationships, leading to poor generalization.
  • C.The vectors become negative in all dimensions.
  • D.The training process terminates immediately.
Show answer

B. The vectors fail to capture meaningful relationships, leading to poor generalization.
Inadequate training data prevents the model from mapping semantic relationships, causing poor downstream performance. Identical embeddings (option 0) and negative vectors (option 2) are not the standard failure modes. Training (option 3) usually continues even if the results are poor.

Take the full Generative AI quiz →

← PreviousPrompt Injection and SafetyNext →Embedding Models — OpenAI, Sentence Transformers

Generative AI

41 lessons, free to read.

All lessons →

Track your progress

Sign in to mark lessons done, score quizzes and keep notes.

Open in the app