تنظيم صفحاتك في مجموعات يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
لنفترض أنّ مجموعة البيانات تحتوي على تصنيف فئوي تكون قيمته إما موجب أو سالب. في مجموعة البيانات المتوازنة، يكون عدد التصنيفات موجبة وسالبة متساوٍ تقريبًا. ومع ذلك، إذا كان أحد التصنيفات أكثر شيوعًا من التصنيف الآخر، تكون مجموعة البيانات غير متوازنة. يُطلق على التصنيف السائد في مجموعة بيانات غير متوازنة اسم الفئة الأكثر تمثيلاً، ويُطلق على التصنيف الأقل شيوعًا اسم الفئة الأقل تمثيلاً.
يقدّم الجدول التالي أسماء ونطاقات مقبولة بشكل عام لقياس درجات عدم التوازن المختلفة:
النسبة المئوية للبيانات التي تنتمي إلى فئة الأقلية
درجة عدم التوازن
من %20 إلى %40 من مجموعة البيانات
معتدل
من %1 إلى %20 من مجموعة البيانات
متوسط
أقل من% 1 من مجموعة البيانات
قصوى
على سبيل المثال، نأخذ مجموعة بيانات لرصد الفيروسات التي تمثل فيها الفئة الأقل تمثيلاً% 0.5 من مجموعة البيانات والفئة الأكثر تمثيلاً %99.5. إنّ مجموعات البيانات غير المتوازنة للغاية مثل هذه المجموعة شائعة في المجال الطبي لأنّه لن يصاب بالفيروس سوى القليل من الأشخاص.
الشكل 5. مجموعة بيانات غير متوازنة للغاية
في بعض الأحيان، لا تحتوي مجموعات البيانات غير المتوازنة على أمثلة كافية للفئة الأقلية لتدريب نموذج بشكل صحيح. وهذا يعني أنّه مع قلة التصنيفات الإيجابية، يتم تدريب النموذج بشكل أساسي على التصنيفات السلبية ولا يمكنه تعلُّم ما يكفي عن التصنيفات الإيجابية. على سبيل المثال، إذا كان حجم المجموعة 50، لن تحتوي العديد من المجموعات على تصنيفات إيجابية.
في أغلب الأحيان، لا يشكّل عدم التوازن مشكلة، خاصةً في مجموعات البيانات التي تتسم بعدم التوازن بشكلٍ بسيط أو معتدل. لذا، عليك محاولة أولاً إجراء عملية التدريب على مجموعة البيانات الأصلية. إذا كان النموذج يعمل بشكل جيد، يعني هذا أنّك انتهيت. وإذا لم يكن الأمر كذلك، يقدّم النموذج غير الأمثل على الأقل مرجعًا جيدًا للتجارب المستقبلية. بعد ذلك، يمكنك تجربة الأساليب التالية للتغلب على المشاكل التي تسبّبها مجموعات البيانات غير المتوازنة.
تصغير نطاق العيّنات وزيادة الوزن
تتمثل إحدى طرق التعامل مع مجموعة بيانات غير متوازنة في تقليل عيّنات الفئة الأكثر تمثيلاً وزيادة وزنها. في ما يلي تعريفات هذين المصطلحين الجديدين:
زيادة الترجيح تعني إضافة ترجيح مثال إلى الفئة التي تمّت خفض عيّنتها بمقدار يساوي العامل الذي تمّت خفض عيّنتها به.
الخطوة 1: تقليل عدد عيّنات الفئة الأكثر شيوعًا فكِّر في مجموعة بيانات الفيروسات الموضّحة في الشكل 5 التي تحتوي على نسبة تصنيف إيجابي واحد لكل 200 تصنيف سلبي. يؤدي تقليل العينة بمقدار 10 إلى تحسين التوازن إلى 1 موجب مقابل 20 سالبًا (%5). على الرغم من أنّ مجموعة التدريب الناتجة لا تزال متوازنة بدرجة معتدلة، فإنّ نسبة الإيجابيات إلى السلبية أفضل بكثير من النسبة الأصلية غير المتوازنة للغاية (0.5%).
الشكل 6. تصغير نطاق العيّنات
الخطوة 2: زيادة وزن الفئة التي تمّ تقليل عيّنتها: أضِف مثالاً على الأوزان إلى الفئة التي تمّ تقليل عيّنتها. بعد تقليل العينة بمقدار 10، يجب أن يكون وزن المثال 10. (قد يبدو هذا الأمر غير منطقي، ولكن سنشرح سبب ذلك في وقت لاحق).
الشكل 7. زيادة الوزن
لا يشير مصطلح weight إلى مَعلمات النموذج (مثل w1 أو w2). يشير الوزن هنا إلى أمثلة على الأوزان، ما يزيد من أهمية المثال الفردي أثناء التدريب. إذا كان مثال الوزن هو 10، يعني ذلك أنّ النموذج يتعامل مع المثال على أنّه أهم 10 مرات (عند احتساب الخسارة) مقارنةً بمثال الوزن 1.
يجب أن يكون الوزن مساويًا للعامل الذي استخدمته لتقليل العينة:
قد يبدو من الغريب إضافة أمثلة على الأوزان بعد تقليل العينة. بعد كلّ شيء، أنت تحاول تحسين أداء النموذج في فئة الأقلية، فلماذا تزيد من أهمية فئة الأغلبية؟ في الواقع، تؤدي زيادة وزن الفئة الأكثر تمثيلاً إلى تقليل التحيز في التنبؤ. وهذا يعني أنّ الزيادة في الوزن بعد تقليل العينة تميل إلى تقليل الفرق بين متوسط توقّعات النموذج ومتوسط تصنيفات مجموعة البيانات.
انقر على الرمز لمعرفة المزيد عن خفض العينة وزيادة الوزن.
قد تتساءل أيضًا عما إذا كان تضخيم القيمة يلغى تأثير تقليل العينة. نعم، إلى حدٍّ ما. ومع ذلك، فإنّ الجمع بين زيادة الوزن وخفض العينة يسمح للمجموعات الصغيرة باحتواء فئات أقلية كافية لتدريب نموذج فعّال.
عادةً ما يكون من الأسهل تنفيذ زيادة وزن الفئة الأقل عددًا بحد ذاتها مقارنةً بتقليل العينة وزيادة وزن الفئة الأكثر عددًا. ومع ذلك، فإنّ زيادة وزن الفئة الأقل تمثيلاً يميل إلى زيادة التحيز في التنبؤ.
توفّر خفض عيّنات فئة الأغلبية المزايا التالية:
الاقتراب بشكل أسرع من الحلّ: أثناء التدريب، يصادف النموذج فئة الأقلية بشكلٍ متكرّر، ما يساعد النموذج على الاقتراب من الحلّ بشكلٍ أسرع.
مساحة أقل على القرص: من خلال دمج فئة الأغلبية في نماذج أقل عددًا ذات أوزان أكبر، يستخدم النموذج مساحة أقل على القرص لتخزين تلك الأوزان. تسمح هذه المساحة المحفوظة بتوفير مساحة أكبر على القرص لفئة الأقلية، وبالتالي يمكن للنموذج جمع عدد أكبر ومجموعة أوسع من الأمثلة من تلك الفئة.
في العادة، يجب تقليل عيّنة فئة الأغلبية يدويًا، ما يمكن أن يستغرق وقتًا طويلاً أثناء تدريب التجارب، لا سيما بالنسبة إلى مجموعات البيانات الكبيرة جدًا.
نِسب إعادة التوازن
ما هو مقدار تقليل العينة وزيادة الوزن المطلوبَين لإعادة التوازن في مجموعة البيانات؟ لتحديد الإجابة، عليك تجربة نسبة إعادة التوازن، تمامًا كما تُجري تجربة على المَعلمات الفائقة الأخرى. ومع ذلك، تعتمد الإجابة في النهاية على العوامل التالية:
حجم الدفعة
نسبة عدم التوازن
عدد الأمثلة في مجموعة التدريب
من الأفضل أن تحتوي كل دفعة على أمثلة متعددة لفئة الأقلية. إنّ الدفعات التي لا تحتوي على فئات أقليات كافية ستؤدي إلى تدريب ضعيف جدًا. يجب أن يكون حجم المجموعة أكبر بعدّة مرات من نسبة عدم التوازن. على سبيل المثال، إذا كانت نسبة عدم التوازن هي 100:1، يجب أن يكون حجم الحزمة 500 على الأقل.
تمرين: التحقّق من فهمك
إليك الموقف التالي:
تحتوي مجموعة التدريب على ما يزيد قليلاً عن مليار مثال.
حجم الدفعة هو 128.
نسبة عدم التوازن هي 100:1، لذا يتم تقسيم مجموعة التدريب على النحو التالي:
حوالي مليار مثال على فئة الأغلبية
10 مليون مثال تقريبًا لفئة الأقليات
أيّ من العبارات التالية صحيحة؟
ستؤدي زيادة حجم الحزمة إلى 1,024 إلى تحسين النموذج الناتج.
عند ضبط حجم الحزمة على 1,024، سيتضمّن كلّ منها ما يقارب 10 أمثلة على فئة الأقلية، ما من شأنه المساعدة في تدريب أفضل بكثير.
سيؤدي إبقاء حجم الحزمة عند 128 ولكن تقليل العينة (وزيادة الوزن) إلى 20:1 إلى تحسين النموذج الناتج.
بفضل خفض العينة، سيتضمّن كل دفعة من 128 صورة ما يقارب 21 مثالاً على فئة الأقلية، ما يكفي لتدريب نموذج مفيد. يُرجى العلم أنّ خفض العينة يقلل من عدد الأمثلة في مجموعة التدريب من أكثر من مليار إلى حوالي 60 مليون.
إنّ المَعلمات الفائقة الحالية جيدة.
عند ضبط حجم الحزمة على 128، سيتضمّن كلّ حزمة في المتوسّط مثالاً واحدًا للفئة الأقلّ تمثيلاً، ما قد لا يكون كافيًا لتدريب نموذج مفعّل.
تاريخ التعديل الأخير: 2025-06-23 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-06-23 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\u003cp\u003eImbalanced datasets occur when one label (majority class) is significantly more frequent than another (minority class), potentially hindering model training on the minority class.\u003c/p\u003e\n"],["\u003cp\u003eDownsampling the majority class and upweighting it can improve model performance by balancing class representation and reducing prediction bias.\u003c/p\u003e\n"],["\u003cp\u003eExperimenting with rebalancing ratios is crucial for optimal performance, ensuring batches contain enough minority class examples for effective training.\u003c/p\u003e\n"],["\u003cp\u003eUpweighting the minority class is simpler but may increase prediction bias compared to downsampling and upweighting the majority class.\u003c/p\u003e\n"],["\u003cp\u003eDownsampling offers benefits like faster convergence and less disk space usage but requires manual effort, especially for large datasets.\u003c/p\u003e\n"]]],[],null,["Consider a dataset containing a categorical label whose value is either\n*Positive* or *Negative* . In a **balanced** dataset, the number of *Positive*\nand *Negative* labels is about equal. However, if one label is more common\nthan the other label, then the dataset is\n[**imbalanced**](/machine-learning/glossary#class_imbalanced_data_set).\nThe predominant label in an imbalanced dataset is called the\n[**majority class**](/machine-learning/glossary#majority_class);\nthe less common label is called the\n[**minority class**](/machine-learning/glossary#minority_class).\n\nThe following table provides generally accepted names and ranges for\ndifferent degrees of imbalance:\n\n| Percentage of data belonging to minority class | Degree of imbalance |\n|------------------------------------------------|---------------------|\n| 20-40% of the dataset | Mild |\n| 1-20% of the dataset | Moderate |\n| \\\u003c1% of the dataset | Extreme |\n\nFor example, consider a virus detection dataset in which the minority class\nrepresents 0.5% of the dataset and the majority class represents 99.5%.\nExtremely imbalanced datasets like this one are common in medicine since\nmost subjects won't have the virus.\n**Figure 5.** Extremely imbalanced dataset.\n\nImbalanced datasets sometimes don't contain enough minority class\nexamples to train a model properly.\nThat is, with so few positive labels, the model trains almost exclusively on\nnegative labels and can't learn enough about positive labels. For example,\nif the batch size is 50, many batches would contain no positive labels.\n\nOften, especially for mildly imbalanced and some moderately imbalanced\ndatasets, imbalance isn't a problem. So, you should **first try\ntraining on the original dataset.** If the model works well, you're done.\nIf not, at least the suboptimal model provides a good\n[**baseline**](/machine-learning/glossary#baseline) for future experiments.\nAfterwards, you can try the following techniques to overcome problems\ncaused by imbalanced datasets.\n\nDownsampling and Upweighting\n\nOne way to handle an imbalanced dataset is to downsample and upweight the\nmajority class. Here are the definitions of those two new terms:\n\n- [**Downsampling**](/machine-learning/glossary#downsampling) (in this context) means training on a disproportionately low subset of the majority class examples.\n- [**Upweighting**](/machine-learning/glossary#upweighting) means adding an example weight to the downsampled class equal to the factor by which you downsampled.\n\n**Step 1: Downsample the majority class.** Consider the virus dataset shown in\nFigure 5 that has a ratio of 1 positive label for every 200 negative labels.\nDownsampling by a factor of 10 improves the balance to 1 positive to 20\nnegatives (5%). Although the resulting training set is still *moderately\nimbalanced* , the proportion of positives to negatives is much better than\nthe original *extremely imbalanced* proportion (0.5%).\n**Figure 6.** Downsampling.\n\n**Step 2: Upweight the downsampled class**: Add example\nweights to the downsampled class. After downsampling by a factor of 10, the\nexample weight should be 10. (Yes, this might seem counterintuitive, but we'll\nexplain why later on.)\n**Figure 7.** Upweighting.\n\nThe term *weight* doesn't refer to model parameters (like, w~1~ or\nw~2~). Here, *weight* refers to\n*example weights*, which increases the importance of an individual example\nduring training. An example weight of 10 means the model treats the example as\n10 times as important (when computing loss) than it would an example of\nweight 1.\n\nThe *weight* should be equal to the factor you used to downsample:\n\n\\\\\\[\\\\text{ \\\\{example weight\\\\} = \\\\{original example weight\\\\} ×\n\\\\{downsampling factor\\\\} }\\\\\\]\n\nIt may seem odd to add example weights after downsampling. After all, you are\ntrying to make the model improve on the minority class, so why upweight the\nmajority class? In fact, upweighting the majority class tends to reduce\n[**prediction bias**](/machine-learning/glossary#prediction-bias). That is,\nupweighting after downsampling tends to reduce the delta between the average\nof your model's predictions and the average of your dataset's labels.\n\nClick the icon to learn more about downsampling and upweighting. \nYou might also be wondering whether upweighting cancels out downsampling.\nYes, to some degree. However, the combination of upweighting and downsampling\nenables [**mini-batches**](/machine-learning/glossary#mini-batch)\nto contain enough minority classes to train an effective model.\n\nUpweighting the *minority class* by itself is usually easier to\nimplement than downsampling and upweighting the *majority class*.\nHowever, upweighting the minority class tends to increase prediction bias.\n\nDownsampling the majority class brings the following benefits:\n\n- **Faster convergence**: During training, the model sees the minority class more often, which helps the model converge faster.\n- **Less disk space**: By consolidating the majority class into fewer examples with larger weights, the model uses less disk space storing those weights. This savings allows more disk space for the minority class, so the model can collect a greater number and a wider range of examples from that class.\n\nUnfortunately, you must usually downsample the majority class manually, which\ncan be time consuming during training experiments, particularly for very large\ndatasets.\n\nRebalance ratios\n\nHow much should you downsample and upweight to rebalance your dataset?\nTo determine the answer, you should experiment with the rebalancing ratio,\njust as you would experiment with other\n[**hyperparameters**](/machine-learning/glossary#hyperparameter).\nThat said, the answer ultimately depends on the following factors:\n\n- The batch size\n- The imbalance ratio\n- The number of examples in the training set\n\nIdeally, each batch should contain multiple minority class examples.\nBatches that don't contain sufficient minority classes will train very poorly.\nThe batch size should be several times greater than the imbalance ratio.\nFor example, if the imbalance ratio is 100:1, then the batch size should\nbe at least 500.\n\nExercise: Check your understanding\n\nConsider the following situation:\n\n- The training set contains a little over one billion examples.\n- The batch size is 128.\n- The imbalance ratio is 100:1, so the training set is divided as follows:\n - \\~1 billion majority class examples.\n - \\~10 million minority class examples.\n\nWhich of the following statements are true? \nIncreasing the batch size to 1,024 will improve the resulting model. \nWith a batch size of 1,024, each batch will average about 10 minority class examples, which should help to train a much better model. \nKeeping the batch size at 128 but downsampling (and upweighting) to 20:1 will improve the resulting model. \nThanks to downsampling, each batch of 128 will average about 21 minority class examples, which should be sufficient for training a useful model. Note that downsampling reduces the number of examples in the training set from a little over one billion to about 60 million. \nThe current hyperparameters are fine. \nWith a batch size of 128, each batch will average about 1 minority class example, which might be insufficient to train a useful model.\n| **Key terms:**\n|\n| - [Baseline](/machine-learning/glossary#baseline)\n| - [Class-imbalanced dataset](/machine-learning/glossary#class_imbalanced_data_set)\n| - [Dataset](/machine-learning/glossary#dataset)\n| - [Downsampling](/machine-learning/glossary#downsampling)\n| - [Hyperparameter](/machine-learning/glossary#hyperparameter)\n| - [Majority class](/machine-learning/glossary#majority_class)\n| - [Mini-batch](/machine-learning/glossary#mini-batch)\n| - [Minority class](/machine-learning/glossary#minority_class)\n| - [Prediction bias](/machine-learning/glossary#prediction-bias)\n- [Upweighting](/machine-learning/glossary#upweighting) \n[Help Center](https://support.google.com/machinelearningeducation)"]]