Einen Transformer-Encoder-Block stapeln
Attention plus Feedforward-Netz und Normalisierungen.
Einen Transformer-Encoder-Block stapeln ist eine kostenlose Deep Learning Academy-Lektion auf CoddyKit. Dies ist Lektion 4 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 Building Block
A transformer is just one encoder block repeated. Learn the block and you understand the whole tower, from tiny models to giant ones.
Two Sub-Layers
Each block has two parts: a multi-head attention sub-layer, then a small feedforward network. Both wrapped with residuals and normalization.
Attention First
The block starts with self-attention, letting every token mix in context from the rest of the sequence before any further processing.
attn_out, _ = self.attn(x, x, x)The Residual Connection
A residual adds the sub-layer's input back to its output. This shortcut lets gradients flow and keeps deep stacks trainable.
x = x + attn_outLayer Normalization
After adding the residual, layer norm rescales each token's features to a stable distribution, steadying training across layers.
x = self.norm1(x)The Feedforward Net
Next comes a position-wise feedforward network: expand to a wider hidden size, apply a nonlinearity, then project back down.
ff = nn.Sequential(nn.Linear(d, 4*d), nn.GELU(), nn.Linear(4*d, d))Per-Token Processing
The feedforward layer treats each token independently. Attention shared information; this step refines each token on its own.
Second Residual and Norm
The feedforward output gets the same treatment: a residual add plus another layer norm, finishing the block.
x = self.norm2(x + ff(x))Stack Them Deep
Stack many identical blocks and the model builds richer representations layer by layer. Depth is where transformer power comes from.
layers = nn.ModuleList([Block(d) for _ in range(N)])Pre-Norm vs Post-Norm
Many modern models apply layer norm before each sub-layer instead of after. Pre-norm trains more stably in very deep stacks.
Use the Built-in
PyTorch gives you nn.TransformerEncoderLayer and nn.TransformerEncoder, so you can assemble a full stack in just a couple of lines.
layer = nn.TransformerEncoderLayer(d_model, nhead)
enc = nn.TransformerEncoder(layer, num_layers=6)Quick Check
Let's review the parts of an encoder block.
Recap
You assembled an encoder block: attention, residual, norm, feedforward, residual, norm. Stack it deep and you have a transformer. Amazing work!
Häufig gestellte Fragen
Ist die Lektion „Einen Transformer-Encoder-Block stapeln“ kostenlos?
Ja — der vollständige Text von „Einen Transformer-Encoder-Block stapeln“ 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 „Einen Transformer-Encoder-Block stapeln“?
Attention plus Feedforward-Netz und Normalisierungen. 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 4 von 4.
Wie lange dauert die Lektion „Einen Transformer-Encoder-Block stapeln“?
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
- Self-Attention: Query, Key und Value
- Skaliertes Skalarprodukt und Multi-Head
- Positionskodierung für die Reihenfolge
- Einen Transformer-Encoder-Block stapeln