Złożyć blok enkodera transformera
Połączyć attention, sieć feedforward i normalizację
Złożyć blok enkodera transformera to bezpłatna lekcja Deep Learning Academy na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Deep Learning Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Deep Learning Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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!
Często zadawane pytania
Czy lekcja „Złożyć blok enkodera transformera” jest bezpłatna?
Tak — pełny tekst „Złożyć blok enkodera transformera” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Deep Learning Academy, przejdź na CoddyKit PRO. Kurs Deep Learning Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Złożyć blok enkodera transformera”?
Połączyć attention, sieć feedforward i normalizację Ćwiczysz Deep Learning Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Deep Learning Academy?
Nie wymagamy żadnego doświadczenia. Deep Learning Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Złożyć blok enkodera transformera”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Deep Learning Academy?
Tak. Każda lekcja Deep Learning Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Self-attention: query, key i value
- Skalowany iloczyn skalarny i multi-head
- Kodowanie pozycyjne dla zachowania kolejności
- Złożyć blok enkodera transformera