Latticework

Command Palette

Search for a command to run...

Embeddings

Vector Representations

18 min

Explanation

An embedding is just a vector — a fixed-length list of numbers — that represents something (a word, an image, a user, a product) in a space where distance and direction carry meaning: similar things end up as nearby vectors. One of the simplest useful operations on embeddings is averaging: combine several item vectors into a single vector that represents, say, "what this user tends to like."

def average_vector(vectors):
    n = len(vectors)
    dim = len(vectors[0])
    result = [0.0] * dim
    for v in vectors:
        for i in range(dim):
            result[i] += v[i]
    return tuple(round(result[i] / n, 4) for i in range(dim))
Try it

This is the same coordinate-wise accumulation as computing a centroid in k-means clustering -- 'average a bunch of vectors together' shows up constantly across ML, not just embeddings.

Loading editor…
Exercise

Write `average_vector(vectors)`: given a list of equal-length numeric vectors (tuples), return their element-wise average as a tuple, each component rounded to 4 decimal places.

Quiz

In a recommendation system, why would you represent a user's preferences as the AVERAGE of the embedding vectors of items they liked?

Checkpoint

You can combine multiple embedding vectors into a single representative vector via element-wise averaging.