Quiz 2

🏗️ Transformers & Attention

207 words
1 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# 🏗️ Transformers & Attention ## 1. 🎯 Learning Objectives - Compute scaled dot-product attention - Explain multi-head attention - Understand positional encoding - Describe the Transformer encoder-decoder architecture ## 2.

🏗️ Transformers & Attention

1. 🎯 Learning Objectives

  • Compute scaled dot-product attention
  • Explain multi-head attention
  • Understand positional encoding
  • Describe the Transformer encoder-decoder architecture

2. 📖 Core Content

3.1 Scaled Dot-Product Attention

Attention(Q,K,V)=softmax(QKTdk)V\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V
  • Q (Query): What am I looking for?
  • K (Key): What do I contain?
  • V (Value): My actual content
  • dkd_k: Dimension of keys (scaling factor prevents softmax saturation)

3.2 Multi-Head Attention

Instead of one attention function, use h heads in parallel:
MultiHead(Q,K,V)=Concat(head1,...,headh)WO\text{MultiHead}(Q, K, V) = \text{Concat}(head_1, ..., head_h)W_O
Each head can focus on different relationships (syntax, semantics, position).

3.3 Positional Encoding

Since self-attention is permutation-invariant (no inherent order), we add positional encodings:
PE(pos,2i)=sin(pos/100002i/dmodel)PE_{(pos, 2i)} = \sin(pos/10000^{2i/d_{model}}) PE(pos,2i+1)=cos(pos/100002i/dmodel)PE_{(pos, 2i+1)} = \cos(pos/10000^{2i/d_{model}})

3.4 Transformer Architecture

(Diagram) Each sublayer has residual connection and layer normalization: LayerNorm(x+Sublayer(x))\text{LayerNorm}(x + \text{Sublayer}(x)).

4. 📝 Practice Questions

Q1: Why is scaling by √d_k needed in attention?
Answer: Without scaling, for large d_k, the dot products grow large, pushing softmax into regions with extremely small gradients (saturating). Scaling ensures more moderate values and better gradient flow. Join Discord PreviousLSTM & GRU DetailsNextSeq2Seq & Attention
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.