Skip to content

Embeddings in AI: Definition, Examples and Code

Quick answer

What embeddings in AI are, how distance shows similarity, a short numpy example, and how they connect to vector databases.

Embeddings in AI: Definition, Examples and Code

An embedding in AI is a list of numbers, called a vector, that represents a piece of data such as a word, sentence or image so that similar items sit close together. Models use embeddings to compare meaning mathematically, which lets a system find related content even when the exact words differ.

Key takeaways

  • An embedding is a vector that represents data in an embedding space.
  • Distance between two embeddings is read as a measure of how similar the items are.
  • Real word embeddings often use 256, 512 or 1024 dimensions, per Google's course.
  • Embeddings are stored and searched in vector databases.

On this page:

TermWhat it isExample
EmbeddingA vector that represents dataA sentence turned into a list of numbers
Vector databaseA store that indexes and searches vectorsHolds embeddings of help articles
TokenA small unit of text a model readsA word or part of a word
Keyword searchMatching exact wordsFinds only pages containing the typed word

What are embeddings in AI?

According to the Google Machine Learning Crash Course, an embedding is a vector representation of data in embedding space. Each item is described by n floating-point numbers.

IBM puts it as numerical representations of data points that convert various types of data, including text and images, into arrays of numbers that models can process.

How do embeddings work?

A model maps each item to a point in space, and related items land near each other. Google says the distance between any two items can be calculated mathematically and read as a measure of relative similarity.

A toy example with made-up numbers in two dimensions: "dosa" is (0.9, 0.1), "idli" is (0.85, 0.2) and "laptop" is (0.05, 0.95). The distance from dosa to idli is about 0.11, while dosa to laptop is about 1.20. Closer means more related. Here is runnable numpy code using the same made-up vectors:

import numpy as np

vecs = {"dosa": np.array([0.9, 0.1]),
        "idli": np.array([0.85, 0.2]),
        "laptop": np.array([0.05, 0.95])}

def cosine(a, b):
    return a @ b / (np.linalg.norm(a) * np.linalg.norm(b))

for other in ("idli", "laptop"):
    print("dosa vs", other, round(float(cosine(vecs["dosa"], vecs[other])), 3))

Cosine similarity compares the angle between vectors and is a common textbook choice. A real vector has hundreds of numbers, not two. Google notes that word embeddings often use 256, 512, or 1024 dimensions.

Where are embeddings used?

They power search, recommendations and retrieval for language models. An illustrative Indian scenario: a student types "how to clear backlog exams" and the system returns a "supplementary exam guide" because their embeddings are close, though no words match.

Embeddings are usually stored in a vector database, covered in best vector databases for RAG, and feed retrieval in RAG.

What are the common mistakes?

  • Expecting each number to mean something. Google says individual dimensions are rarely as interpretable as examples like "dessertness".
  • Mixing embeddings from different models. Vectors from two models live in different spaces and cannot be compared directly.
  • Confusing an embedding with the database that stores it. One is the data, the other is the storage and search system.

Vector databases, tokens and similarity search are close neighbours. For context, read what an LLM is, maths for machine learning and the large language models guide.

Next step

Run the snippet, then invent a fourth word and place it where you think it belongs. Check whether the similarity scores match your intuition.

Frequently asked questions

What are embeddings in AI?

An embedding is a vector representation of data in embedding space. Text, images and other data are converted into lists of numbers so that similar items sit close together.

How are embeddings different from a vector database?

An embedding is the list of numbers for one item. A vector database stores, manages and indexes many such vectors so similar ones can be found quickly.

How is similarity between two embeddings measured?

Google's course says the distance between two items can be calculated mathematically and read as relative similarity. Cosine similarity, which compares angles, is a common choice.

Why do embeddings have hundreds of numbers?

More dimensions let a model capture more detail about an item. Google says word embeddings often use 256, 512 or 1024 dimensions, and single dimensions are rarely easy to interpret.

Share LinkedIn X WhatsApp
×

Our Courses

Practice-Based Learning Tracks, Supercharged By A.I.