EasyDeepLearn

How does self-attention work in a transformer?

hard

Answer

  • For each token, produce a Query, Key, and Value vector.
  • The output for token i is a weighted sum of all Values, where the weight is softmax(Qi    Kj  /  sqrt(dk))\operatorname{softmax}(Q_{i}\; \cdot \;K_{j}\; / \;\mathrm{sqrt}(d_{k})).
  • Multi-head attention runs this in parallel with different projections and concatenates the results.
  • Attention lets every token look at every other token — capturing long-range dependencies with O(n2)O(n^{2}) complexity.
Check yourself — multiple choice
  • Attention weights are computed as Q · V
  • Weights are softmax(Q    K  /  sqrt(dk))\operatorname{softmax}(Q\; \cdot \;K\; / \;\mathrm{sqrt}(d_{k})) applied to V
  • Attention only looks at neighboring tokens
  • Multi-head means multiple layers stacked in series

That's the scaled dot-product attention formula.

#attention#transformers

Practise Deep Learning

214 interview questions in this topic.

Related questions