0Pricing
Deep Learning Academy · Lektion

Skaliertes Skalarprodukt und Multi-Head

Führen Sie Attention parallel in mehreren Teilräumen aus.

Skaliertes Skalarprodukt und Multi-Head ist eine kostenlose Deep Learning Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Deep Learning Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Deep Learning Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.5

Scaled 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 @ v

One 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_heads

Reshape 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!

Häufig gestellte Fragen

Ist die Lektion „Skaliertes Skalarprodukt und Multi-Head“ kostenlos?

Ja — der vollständige Text von „Skaliertes Skalarprodukt und Multi-Head“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Deep Learning Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Deep Learning Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Skaliertes Skalarprodukt und Multi-Head“?

Führen Sie Attention parallel in mehreren Teilräumen aus. Du übst Deep Learning Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Deep Learning Academy zu starten?

Keine Vorkenntnisse erforderlich. Deep Learning Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Skaliertes Skalarprodukt und Multi-Head“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Deep Learning Academy-Lektion Code schreiben und ausführen?

Ja. Jede Deep Learning Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Self-Attention: Query, Key und Value
  2. Skaliertes Skalarprodukt und Multi-Head
  3. Positionskodierung für die Reihenfolge
  4. Einen Transformer-Encoder-Block stapeln
← Zurück zu Deep Learning Academy