Entropia krzyżowa dla wielu klas
Zrozumieć, dlaczego oczekuje surowych logitów, a nie softmax
Entropia krzyżowa dla wielu klas 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.
More Than Two Choices
When your model must pick one label from many, like a digit 0 through 9, you use cross-entropy, the standard multiclass classification loss. 🔢
One Logit Per Class
For C classes, your network outputs a vector of C raw scores called logits, one per possible class, for every sample in the batch.
Softmax Turns Scores Into Odds
Softmax exponentiates each logit and normalizes them so they sum to 1, turning raw scores into a clean probability distribution.
import torch
probs = torch.softmax(logits, dim=1)The Loss for Many Classes
PyTorch bundles softmax and the loss into nn.CrossEntropyLoss. It scores how much probability your model put on the correct class.
import torch.nn as nn
loss_fn = nn.CrossEntropyLoss()Pass Raw Logits Directly
This is the key rule: feed CrossEntropyLoss the raw logits, never a softmax output. It applies log-softmax internally for you.
loss = loss_fn(logits, labels)Why Not Softmax First
Applying softmax yourself, then this loss, squashes the values twice. That double pass distorts gradients and quietly wrecks training.
Labels Are Class Indices
Your targets are plain integer indices, like 3 for class three, not one-hot vectors. PyTorch handles the lookup for you.
labels = torch.tensor([3, 0, 7])Mind the Output Shape
Logits have shape batch by classes, while labels have shape batch only. A shape mismatch here is the most common cross-entropy error.
It Is Built From Two Pieces
Under the hood, CrossEntropyLoss equals log-softmax followed by negative log-likelihood. Fusing them keeps the math numerically stable.
Predict With Argmax
To get the final answer, take the argmax over the logits. The largest score is the predicted class, no softmax needed.
preds = logits.argmax(dim=1)Smooth Labels for Generalization
Setting label_smoothing nudges targets slightly away from a hard 1, which discourages overconfidence and often improves test accuracy.
loss_fn = nn.CrossEntropyLoss(label_smoothing=0.1)Quick Check
You are using nn.CrossEntropyLoss for 10 classes. What should the model's final output be?
Recap: One Winner Per Sample
For multiclass tasks you feed raw logits and integer labels to CrossEntropyLoss, which softmaxes internally and scores the right class. Argmax gives the prediction. 🏆
Często zadawane pytania
Czy lekcja „Entropia krzyżowa dla wielu klas” jest bezpłatna?
Tak — pełny tekst „Entropia krzyżowa dla wielu klas” 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 „Entropia krzyżowa dla wielu klas”?
Zrozumieć, dlaczego oczekuje surowych logitów, a nie softmax Ć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 „Entropia krzyżowa dla wielu klas”?
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
- MSE i MAE dla regresji
- Binarna entropia krzyżowa z logitami
- Entropia krzyżowa dla wielu klas
- Wagi klas dla niezrównoważonych danych