skipLink.label

Quest 10 - Position Encoding Explorer

Quest 10: Position Encoding Explorer

medium 25 minutes

🎯 Learning Objectives

  • ✅ Why transformers need positional encoding (attention is permutation-invariant)
  • ✅ How sinusoidal positional encoding works
  • ✅ The sin/cos pattern for even/odd dimensions
  • ✅ The engineering habit: SEQUENCE MATTERS

📖 Concept: Positional Encoding

Attention mechanism ไม่รู้ลำดับของ tokens — มันมองว่า [“I”, “love”, “cats”] กับ [“cats”, “love”, “I”] เหมือนกัน

Positional encoding แก้ปัญหานี้โดยเพิ่มข้อมูลตำแหน่งเข้าไปใน embeddings ทำให้ model รู้ว่า token แต่ละตัวอยู่ตรงไหน


⚙️ How It Works

Sinusoidal Formula

PE(pos, 2i) = sin(pos / 10000^(2i/d_model))
PE(pos, 2i+1) = cos(pos / 10000^(2i/d_model))
  • pos = ตำแหน่งของ token (0, 1, 2, …)
  • i = dimension index (0, 1, 2, …)
  • d_model = ขนาด embedding dimension

Pattern

Dimension 0 (even) -> sin
Dimension 1 (odd) -> cos
Dimension 2 (even) -> sin
Dimension 3 (odd) -> cos
...

💡 Example: Positional Encoding in Practice

function positionalEncoding(maxLen, dModel) {
const pe = [];
for (let pos = 0; pos < maxLen; pos++) {
const row = [];
for (let i = 0; i < dModel; i++) {
const angle = pos / Math.pow(10000, (2 * Math.floor(i / 2)) / dModel);
// Even index -> sin, odd index -> cos
if (i % 2 === 0) {
row.push(Math.sin(angle));
} else {
row.push(Math.cos(angle));
}
}
pe.push(row);
}
return pe;
}
// Example: 3 positions, 4 dimensions
const pe = positionalEncoding(3, 4);
// Position 0: [sin(0), cos(0), sin(0), cos(0)] = [0, 1, 0, 1]
// Position 1: [sin(1), cos(1), sin(1), cos(1)] = [0.84, 0.54, 0.84, 0.54]
// Position 2: [sin(2), cos(2), sin(2), cos(2)] = [0.91, -0.42, 0.91, -0.42]

⚠️ Common Mistakes

Mistake 1: สลับ sin/cos

ใช้ sin กับ odd dimensions, cos กับ even -> ต้อง: even (0,2,4,…) -> sin, odd (1,3,5,…) -> cos

Mistake 2: คำนวณ angle ผิด

ใช้ pos * i / dModel แทน pos / 10000^(2i/dModel) -> ต้องใช้ formula ที่ถูกต้อง

Mistake 3: ไม่ handle dModel ไม่เท่ากับ 2

คิดว่า dModel ต้องเป็นเลขคู่เสมอ -> ทำงานได้กับ dModel ใดก็ได้

Mistake 4: ไม่ test กับ maxLen = 1

ไม่ test edge case -> ต้อง test กับ maxLen = 1, dModel = 1


📝 Knowledge Check

📝 Knowledge Check

Q1:Why do transformers need positional encoding?

Q2:In sinusoidal positional encoding, which dimension indices use sin vs cos?

Q3:What is the formula for the angle at position pos and dimension i?


🏋️ Quest: Position Encoding Explorer

เขียน function ที่คำนวณ positional encoding ด้วย sin/cos

  1. Download ไฟล์เริ่มต้นของ quest:

    Terminal window
    npx bluebeltdojo download quest-10-position-encoding
    cd quest-10-position-encoding
  2. เปิด problem.js ใน editor ของคุณพร้อม AI tool

  3. ให้ AI ช่วย implement positionalEncoding(maxLen, dModel) ตาม formula

  4. ตรวจสอบ:

    Terminal window
    node test.js
  5. ทดสอบ: sin/cos pattern, edge cases

  6. ส่งคำตอบ:

    Terminal window
    npx bluebeltdojo submit

💡 Tip: ถ้า AI สลับ sin/cos ให้บอกว่า “Even indices (0,2,4) should use sin, odd indices (1,3,5) should use cos”


คำใบ้

  • อ่าน instructions ใน problem.js อย่างละเอียด — มี formula ระบุไว้
  • ตรวจสอบ sin/cos pattern: even -> sin, odd -> cos
  • ทดสอบกับ maxLen = 1, dModel = 1
  • ถ้าติดขัด ลอง search “sinusoidal positional encoding transformer”