Self-Attention: Trace One Row
- Queries and keys choose weights; values supply the information mixed.
- Score row i belongs to query i; column j belongs to key/value j.
- This v1 exercise implements one head with scalar autograd.
1 / 7
x,Q,K,V: (T,D) in the module.scores: (T,T) with divisor sqrt(d_k).apply_attention: weights (T,T) times values (T,d_v).SelfAttention also applies its output projection.Q=[[0],[1]], K=[[0],[log(3)]], V=[[2],[6]]
[0.5,0.5], output 4.[0.25,0.75], output 5.[1,0], output 2.[1000,1001,1002] -> [-2,-1,0].(s-m).exp(), not Value(exp(s.data-m)).[0,0] and values [2,6], score gradients are [-1,1].-1e9 for demonstration.1e12 remains dominant after that penalty.row[:i+1].With identity scalar projections and inputs [1,1e12]:
[1,0].O(T^2*D) time.O(T*D^2).