0Pricing
Deep Learning Academy · Lekcja

Wagi klas dla niezrównoważonych danych

Zapobiegać dominacji klasy większościowej

Wagi klas dla niezrównoważonych danych 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.

When One Class Dominates

If 95 percent of your samples are one class, the model can score high by always guessing it. That is the class imbalance trap. ⚖️

Why Accuracy Lies Here

A lazy model that ignores the rare class still looks 95 percent accurate. So accuracy hides total failure on the minority you actually care about.

The Loss Follows the Majority

Each sample contributes equally to the loss, so the abundant class pulls the gradient hardest. The model learns mostly from the majority class.

Reweight the Loss

The fix is to give rare classes a heavier voice. Class weights multiply each sample's loss so minority mistakes count for more.

Bigger Weight for Rarer Class

A common recipe sets each weight inversely proportional to its class frequency. The rarer the class, the larger its weight grows.

Pass Weights to CrossEntropy

For multiclass tasks, hand a weight tensor of length C to the loss. Each entry scales the penalty for that class's errors.

import torch.nn as nn
w = torch.tensor([1.0, 9.0])
loss_fn = nn.CrossEntropyLoss(weight=w)

pos_weight for Binary Tasks

For two-class problems, BCEWithLogitsLoss takes pos_weight, a single number that boosts the positive class to offset its rarity.

loss_fn = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([9.0]))

Sample, Don't Just Weight

Weighting is not the only tool. You can also oversample the rare class or undersample the common one to balance each batch.

Balance Batches With a Sampler

A WeightedRandomSampler draws minority samples more often, so every batch the DataLoader serves is roughly balanced by design.

from torch.utils.data import WeightedRandomSampler

Focal Loss for Hard Cases

For extreme imbalance, focal loss down-weights easy, well-classified examples so the model focuses on the hard, rare ones.

Judge by the Right Metric

After rebalancing, drop accuracy and track F1 or precision and recall. They reveal whether the rare class is finally being learned.

Quick Check

Your dataset is 95 percent negative. How do you stop the model from ignoring positives?

Recap: Give the Rare Class a Voice

Imbalance lets the majority hijack training, so you fight back with class weights, balanced sampling, or focal loss, and you judge results with F1. 💪

Często zadawane pytania

Czy lekcja „Wagi klas dla niezrównoważonych danych” jest bezpłatna?

Tak — pełny tekst „Wagi klas dla niezrównoważonych danych” 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 „Wagi klas dla niezrównoważonych danych”?

Zapobiegać dominacji klasy większościowej Ć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 „Wagi klas dla niezrównoważonych danych”?

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

  1. MSE i MAE dla regresji
  2. Binarna entropia krzyżowa z logitami
  3. Entropia krzyżowa dla wielu klas
  4. Wagi klas dla niezrównoważonych danych
← Powrót do Deep Learning Academy