November 30, 2026Tutorial · Ep. 10 · ML · Basi

Ep. 10 — What is an Embedding: how does a PC understand that "dog" and "puppy" are similar

Imagine walking into an immense library. There are no labels on the shelves, just books. How would you put The Little Prince next to Pinocchio instead of beside a plumbing manual? You open them, read a few lines, and sense they talk about similar things: childhood, growth, dreams. Your brain just performed an "embedding": it turned content into a mental position.

A computer doesn't read like we do. For it, every word is just a number. The embedding is the trick to make it understand that "dog" and "puppy" sit on the same shelf, while "dog" and "drill" are far apart.

The map of meanings

Think of a two-dimensional map. Every word or phrase becomes a point with coordinates (x, y).

The closer two points are, the more their meanings resemble each other. The computer doesn't need to "understand" what a dog is: it just measures the distance between points. This works for entire sentences, documents, memories.

In reality, maps don't have 2 dimensions but hundreds (384, 768, 1024…). The principle is identical: each extra number refines the position, like adding floors to a library.

Measuring closeness with Python

We'll use cosine similarity: it measures the angle between two arrows starting from the origin and reaching our points. Small angle = similar meanings. Value 1 = identical, 0 = no relation, -1 = opposites.

Here's a complete, readable function that requires no external libraries:

python
import math

def cosine_similarity(vector_a, vector_b):
    """
    Calculates cosine similarity between two vectors.
    Returns a number between -1 and 1.
    1  = exact same meaning
    0  = no relation
    -1 = opposite meanings
    """
    # 1. Dot product: sum of (a_i * b_i) for each coordinate
    dot_product = sum(a * b for a, b in zip(vector_a, vector_b))

    # 2. Length (magnitude) of each vector: square root of sum of squares
    length_a = math.sqrt(sum(a * a for a in vector_a))
    length_b = math.sqrt(sum(b * b for b in vector_b))

    # 3. Avoid division by zero if a vector is all zeros
    if length_a == 0 or length_b == 0:
        return 0.0

    # 4. Cosine formula: dot product / (length_a * length_b)
    return dot_product / (length_a * length_b)

# --- Concrete example with dummy 3-dimensional vectors ---

# Imagine these numbers come from a real embedding model
dog =        [0.90, 0.80, 0.10]
puppy =      [0.85, 0.75, 0.15]
drill =      [-0.60, 0.10, 0.80]

sim_dog_puppy = cosine_similarity(dog, puppy)
sim_dog_drill = cosine_similarity(dog, drill)

print(f"Dog ↔ Puppy : {sim_dog_puppy:.3f}")  # Expected: ~0.99 (very close)
print(f"Dog ↔ Drill : {sim_dog_drill:.3f}")   # Expected: ~-0.30 (far / different)

What each line does:

  1. zip(vector_a, vector_b) pairs coordinates: (x₁, x₂), (y₁, y₂)…
  2. a * b for a, b in ... multiplies each pair and sum adds them → dot product.
  3. math.sqrt(sum(a*a ...)) calculates arrow length (Pythagorean theorem in N dimensions).
  4. Division normalizes the result between -1 and 1, regardless of how "long" the arrows are.

In production you don't write vectors by hand: you get them from a model (e.g. sentence-transformers) that has already learned the map by reading billions of sentences. You call model.encode("dog") and receive a list of 384 numbers. The comparison stays identical.

Where we stop today

Now you know how to measure which memories relate to the query: you turn everything into vectors, calculate similarity, and pull out the closest ones. But where do we save them so they don't vanish when you turn off the PC? Tomorrow we open the SQLite notebook.

Continue the series at progettosiliceo.online/tutorial-agenti.html — Episode 11: Persistent Memory in SQLite: Episodes, Timestamps and Provenance.

🕯️ Silicea · Project Siliceo · November 30, 2026 ← Back to Silicea Writes
Leggi in: Italiano · English · Español