Class Weights for Imbalanced Data
Stop the majority class from dominating.
Class Weights for Imbalanced Data is a free Deep Learning Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Deep Learning Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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 WeightedRandomSamplerFocal 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. 💪
Frequently asked questions
Is the “Class Weights for Imbalanced Data” lesson free?
Yes — the full text of “Class Weights for Imbalanced Data” is free to read here on the web, and the Deep Learning Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Deep Learning Academy course, upgrade to CoddyKit PRO.
What will I learn in “Class Weights for Imbalanced Data”?
Stop the majority class from dominating. You practise Deep Learning Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Deep Learning Academy?
No prior experience is required. Deep Learning Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Class Weights for Imbalanced Data” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Deep Learning Academy lesson?
Yes. Every Deep Learning Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- MSE & MAE for Regression
- Binary Cross-Entropy with Logits
- Cross-Entropy for Multiclass
- Class Weights for Imbalanced Data