Scaled Dot-product Attention
A query scores keys; stable softmax turns scores into weights, then a weighted sum mixes values. Inspect scores, weights and the output vector.
weights = [0.25, 0.75]
values = [[2, 0], [0, 2]]
print([sum(w*v[j] for w,v in zip(weights, values)) for j in range(2)])From arithmetic to an experiment
A query scores keys; stable softmax turns scores into weights, then a weighted sum mixes values. Inspect scores, weights and the output vector. The trace reads the local variables of your function as it executes. Change one input or parameter, predict the result, then run the checks again. A small deterministic example makes arithmetic mistakes easier to locate than a long training run. State the shape and meaning of the returned value before coding. Keep preprocessing, evaluation and model updates in their proper order, and examine the boundary case in each check. The companion lab lets you replay intermediate values while the same algorithm runs.
Your task
attend(query, keys, values) returns one attention output vector using dot products divided by sqrt(len(query)), followed by stable softmax. keys is nonempty, dimensions match, each value has the same width.
The explanation opens here once every check passes.