from math import exp, sqrt q = [1.0, 0.0] keys = [[1.0, 0.0], [0.0, 1.0]] values = [[10.0, 0.0], [0.0, 10.0]] scores = [sum(a*b for a, b in zip(q, k))/sqrt(2) for k in keys] e = [exp(s-max(scores)) for s in scores] weights = [x/sum(e) for x in e] out = [sum(w*v[j] for w, v in zip(weights, values)) for j in range(2)] print("Weights:", [round(w, 3) for w in weights]) print("Output:", [round(x, 3) for x in out])