Deep Learning Academy · درس

أوزان الفئات للبيانات غير المتوازنة

امنع الفئة الأكبر من الهيمنة

الدرس 4 من 413 خطوة

أوزان الفئات للبيانات غير المتوازنة درس مجاني في Deep Learning Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 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. 💪

البدء مجانًا

تعلم Python مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
30
الدروس
120

الأسئلة الشائعة

هل درس «أوزان الفئات للبيانات غير المتوازنة» مجاني؟

نعم — نص درس «أوزان الفئات للبيانات غير المتوازنة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Deep Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.

ماذا ستتعلم في «أوزان الفئات للبيانات غير المتوازنة»؟

امنع الفئة الأكبر من الهيمنة تتمرن على Deep Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Deep Learning Academy؟

لا تُشترط خبرة سابقة. Deep Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «أوزان الفئات للبيانات غير المتوازنة»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Deep Learning Academy هذا؟

نعم. كل درس في Deep Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. ‏MSE وMAE للانحدار
  2. ‏Binary Cross-Entropy مع Logits
  3. ‏Cross-Entropy للتصنيف متعدد الفئات
  4. أوزان الفئات للبيانات غير المتوازنة
← العودة إلى Deep Learning Academy