4/25
Part 1 of 6 · The lookupLearn and do

One query scores every key, then blends the values

Here's one lookup in NumPy: one query, three keys.

import numpy as np


def softmax(s, axis=-1):
    e = np.exp(s)
    return e / e.sum(axis=axis, keepdims=True)


keys = np.array([[1.0, 0.0],
                 [0.0, 1.0],
                 [1.0, 1.0]])
values = np.array([10.0, 20.0, 30.0])
query = np.array([1.0, 2.0])

scores = np.array([key @ query for key in keys])
weights = softmax(scores)
print(scores)
print(weights.round(2))
print((weights * values).sum().round(1))
Predict

Query [1, 2] against keys [1, 0], [0, 1] and [1, 1]. Which key gets the biggest weight?

Pick one to see what it prints.
Pick an answer above to see the rest