Binary Cross-Entropy مع Logits
الخسارة المستقرة لمسائل الفئتين
Binary Cross-Entropy مع Logits درس مجاني في Deep Learning Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Deep Learning Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Two Classes, One Decision
When your model answers a yes or no question like spam or not spam, you reach for binary cross-entropy, the loss built for two-class problems. ✅
From Score to Probability
A model first outputs a raw score called a logit. The sigmoid function squashes that logit into a probability between 0 and 1.
import torch
prob = torch.sigmoid(logit)What Cross-Entropy Rewards
Cross-entropy gives a small loss when the predicted probability is close to the true label, and a large loss when it is confidently wrong.
Confident and Wrong Hurts
Predicting 0.99 when the true label is 0 produces a huge loss. Cross-entropy punishes confident mistakes far more than uncertain ones.
The Naive Loss Choice
You could apply sigmoid yourself, then use nn.BCELoss on the probabilities. It works, but there is a safer way for real training.
import torch.nn as nn
loss_fn = nn.BCELoss()
loss = loss_fn(prob, target)Feed Logits, Not Probabilities
The preferred loss is BCEWithLogitsLoss. It takes raw logits directly and applies the sigmoid internally for you.
import torch.nn as nn
loss_fn = nn.BCEWithLogitsLoss()
loss = loss_fn(logit, target)Why Combine the Steps
Fusing sigmoid and the loss uses the log-sum-exp trick, which avoids overflow and gives stable gradients even at extreme logits.
Skip the Extra Sigmoid
Because BCEWithLogitsLoss applies sigmoid itself, your model's last layer should output raw logits with no sigmoid attached.
Targets Are 0 or 1
Pass float targets of 0.0 or 1.0 with the same shape as your logits. A mismatched shape is the classic BCE bug to watch for.
target = torch.tensor([1.0, 0.0, 1.0])Sigmoid Only at Inference
During training the loss handles the squashing. To read a probability for a prediction, apply sigmoid to the logit at inference time.
Tilt the Balance with pos_weight
If positives are rare, the pos_weight argument scales up their contribution so the model stops ignoring the minority class.
Quick Check
You are doing binary classification and want numerical stability. What should you feed the loss?
Recap: Stable Yes or No
For two-class tasks you feed raw logits to BCEWithLogitsLoss, which squashes and scores in one stable step. Save sigmoid for reading probabilities later. 👍
الأسئلة الشائعة
هل درس «Binary Cross-Entropy مع Logits» مجاني؟
نعم — نص درس «Binary Cross-Entropy مع Logits» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Deep Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
ماذا ستتعلم في «Binary Cross-Entropy مع Logits»؟
الخسارة المستقرة لمسائل الفئتين تتمرن على Deep Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Deep Learning Academy؟
لا تُشترط خبرة سابقة. Deep Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «Binary Cross-Entropy مع Logits»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Deep Learning Academy هذا؟
نعم. كل درس في Deep Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- MSE وMAE للانحدار
- Binary Cross-Entropy مع Logits
- Cross-Entropy للتصنيف متعدد الفئات
- أوزان الفئات للبيانات غير المتوازنة