PythiloLesson 2 of 9 · progress kept in this browser

Embeddings

Inside a model every token becomes a vector, an embedding. Training places tokens used in similar ways close together, so meaning becomes direction.

The usual way to compare two vectors is cosine similarity: the cosine of the angle between them. It is 1 for the same direction, 0 for unrelated, -1 for opposite, and it ignores length.

import torch
import torch.nn.functional as F

a = torch.tensor([1.0, 2.0])
b = torch.tensor([2.0, 4.0])
print(F.cosine_similarity(a, b, dim=0))
# tensor(1.): same direction

Search engines, recommendation and the retrieval in chat assistants all start here: turn things into vectors, then find the nearest ones.

Your task

Write cosine(a, b) returning the cosine similarity of two 1-D tensors as a float. Write nearest(query, matrix, k) returning the indices of the k rows of matrix most similar to query, best first, as a list of ints.

This lesson uses torch, which Python in this browser does not include. Read it here, and run and check it in the Pythilo app on iPhone or iPad.

Next: Retrieval