Transformer Self-Attention & Softmax

Scaled dot-product self-attention lets every Transformer token dynamically weight information from all visible context tokens by dividing QK^T dot products by sqrt(d_k) before row-wise softmax.

Technical Reference & Equations

  • Scaled Dot-Product Attention: \mathrm{Attention}(Q, K, V) = \mathrm{softmax}\!\left(\frac{Q K^\top}{\tau \sqrt{d_k}} + M\right) V (sqrt(d_k) prevents large dot products from saturating softmax gradients; M masks future tokens.)
  • Row-Wise Temperature Softmax: \alpha_{i,j} = \frac{\exp(s_{i,j} / \tau)}{\sum_{k} \exp(s_{i,k} / \tau)} (Lower temperature tau sharpens focus toward the top key; higher tau flattens weights.)
  • Attention Entropy & Effective Context (Perplexity): H_i = -\sum_j \alpha_{i,j} \log_2 \alpha_{i,j}, \quad \mathrm{PPL}_i = 2^{H_i} (Effective number of tokens actively attended to by query token i.)

View full Machine Learning Formula Sheet | Constants Table

Related Machine Learning Calculators