ผลคูณจุดแบบปรับสเกลและหลายหัว
ให้ความสนใจในปริภูมิย่อยแบบขนาน
ผลคูณจุดแบบปรับสเกลและหลายหัว เป็นบทเรียน Deep Learning Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Deep Learning Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Scaling Problem
When key vectors are long, dot-product scores grow huge and softmax becomes razor-sharp. That kills gradients, so we need to scale the scores down.
Divide by Root d
The fix is to divide each score by the square root of the key dimension. This keeps the numbers in a stable range before softmax.
scores = (q @ k.transpose(-2, -1)) / d_k ** 0.5Scaled Dot-Product Attention
Put it together: score, scale, softmax, then blend values. This four-step recipe is the famous scaled dot-product attention.
w = scores.softmax(dim=-1)
out = w @ vOne Head, One View
A single attention head learns just one way to relate tokens. But language has many relationships at once, so one head is limiting.
Many Heads in Parallel
Multi-head attention runs several attention heads side by side, each with its own Q, K, V projections, each attending in a different subspace.
Split the Dimension
You do not make the model wider. You split the model dimension across heads, so each head works on a smaller slice in parallel.
d_head = d_model // num_headsReshape into Heads
To run heads in parallel, you reshape Q, K, V to add a heads axis, then attend within each head independently.
q = q.view(B, T, H, d_head).transpose(1, 2)Different Patterns
One head may track grammar, another may link a pronoun to its noun. Multiple heads capture diverse patterns the same input contains.
Concatenate Heads
After each head produces its output, you concatenate them back into one vector of the original model dimension.
out = out.transpose(1, 2).reshape(B, T, d_model)Final Projection
A last output projection mixes the concatenated head results, letting the model combine what every head discovered.
out = self.W_o(out)Use the Built-in
In practice PyTorch ships nn.MultiheadAttention, so you rarely hand-roll the reshapes. Knowing the math still helps you debug.
attn = nn.MultiheadAttention(d_model, num_heads)Quick Check
Let's confirm why we scale the scores.
Recap
You saw how scaling stabilizes softmax and how multi-head attention splits the work across parallel heads, then concatenates and projects. Great progress!
คำถามที่พบบ่อย
บทเรียน “ผลคูณจุดแบบปรับสเกลและหลายหัว” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ผลคูณจุดแบบปรับสเกลและหลายหัว” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Deep Learning Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ผลคูณจุดแบบปรับสเกลและหลายหัว”
ให้ความสนใจในปริภูมิย่อยแบบขนาน คุณปฏิบัติ Deep Learning Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Deep Learning Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Deep Learning Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ผลคูณจุดแบบปรับสเกลและหลายหัว” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Deep Learning Academy นี้ได้ไหม
ได้ บทเรียน Deep Learning Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ความสนใจในตัวเอง: คิวรี คีย์ และแวลู
- ผลคูณจุดแบบปรับสเกลและหลายหัว
- การเข้ารหัสตำแหน่งเพื่อบอกลำดับ
- วางบล็อกตัวเข้ารหัส Transformer ซ้อนกัน