น้ำหนักคลาสสำหรับข้อมูลไม่สมดุล
หยุดไม่ให้คลาสส่วนใหญ่ครอบงำ
น้ำหนักคลาสสำหรับข้อมูลไม่สมดุล เป็นบทเรียน Deep Learning Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Deep Learning Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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. 💪
คำถามที่พบบ่อย
บทเรียน “น้ำหนักคลาสสำหรับข้อมูลไม่สมดุล” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “น้ำหนักคลาสสำหรับข้อมูลไม่สมดุล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Deep Learning Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “น้ำหนักคลาสสำหรับข้อมูลไม่สมดุล”
หยุดไม่ให้คลาสส่วนใหญ่ครอบงำ คุณปฏิบัติ Deep Learning Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Deep Learning Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Deep Learning Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “น้ำหนักคลาสสำหรับข้อมูลไม่สมดุล” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Deep Learning Academy นี้ได้ไหม
ได้ บทเรียน Deep Learning Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- MSE และ MAE สำหรับการถดถอย
- Binary Cross-Entropy พร้อม Logits
- Cross-Entropy สำหรับหลายคลาส
- น้ำหนักคลาสสำหรับข้อมูลไม่สมดุล