Kodowanie pozycyjne dla zachowania kolejności
Wprowadzać pozycję w sekwencji do tokenów
Kodowanie pozycyjne dla zachowania kolejności to bezpłatna lekcja Deep Learning Academy na CoddyKit. To lekcja 3 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.
Attention Ignores Order
Self-attention treats a sentence like a bag of tokens. Shuffle the words and the math barely changes, so the model has no built-in sense of order.
Order Carries Meaning
But order matters: "dog bites man" is not "man bites dog". We must hand the model some signal about each token's position.
Add, Do Not Append
The trick is to add a position vector directly to each token's embedding. Same shape, so attention sees content and place fused together.
x = token_emb + pos_embSinusoidal Encoding
The original transformer used fixed sine and cosine waves of different frequencies, one pattern per dimension, to mark each position.
Why Sine Waves
Mixing frequencies gives every position a unique fingerprint, and the smooth waves let the model generalize to lengths it never saw in training.
The Formula
Even dimensions use sine, odd dimensions use cosine, with the wavelength growing across dimensions. That is the whole encoding recipe.
pe[:, 0::2] = torch.sin(pos / div)
pe[:, 1::2] = torch.cos(pos / div)Relative Distance
A neat bonus: with sinusoids, the offset between two positions is easy to express, helping attention reason about relative distance.
Learned Positions
Modern models often skip sinusoids and use a learned embedding table, one trainable vector per position, just like word embeddings.
self.pos_emb = nn.Embedding(max_len, d_model)Fixed vs Learned
Fixed sinusoids extrapolate to new lengths for free; learned tables fit your data but are capped at the maximum length you trained on.
Rotary Encoding
Newer transformers favor rotary position encoding, which rotates query and key vectors by an angle tied to position, baking order into attention itself.
Where It Goes
Whatever scheme you pick, positional info is injected once at the input, before the first attention layer ever runs.
Quick Check
Let's check why positional encoding exists.
Recap
You learned that attention ignores order, so we add positional encodings, whether sinusoidal, learned, or rotary, to tell tokens where they sit. Well done!
Często zadawane pytania
Czy lekcja „Kodowanie pozycyjne dla zachowania kolejności” jest bezpłatna?
Tak — pełny tekst „Kodowanie pozycyjne dla zachowania kolejności” 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 „Kodowanie pozycyjne dla zachowania kolejności”?
Wprowadzać pozycję w sekwencji do tokenów Ć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 3 z 4.
Ile czasu zajmuje lekcja „Kodowanie pozycyjne dla zachowania kolejności”?
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