Cross-Entropy للتصنيف متعدد الفئات
لماذا تتوقع Logits خامًا لا Softmax
Cross-Entropy للتصنيف متعدد الفئات درس مجاني في Deep Learning Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Deep Learning Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
More Than Two Choices
When your model must pick one label from many, like a digit 0 through 9, you use cross-entropy, the standard multiclass classification loss. 🔢
One Logit Per Class
For C classes, your network outputs a vector of C raw scores called logits, one per possible class, for every sample in the batch.
Softmax Turns Scores Into Odds
Softmax exponentiates each logit and normalizes them so they sum to 1, turning raw scores into a clean probability distribution.
import torch
probs = torch.softmax(logits, dim=1)The Loss for Many Classes
PyTorch bundles softmax and the loss into nn.CrossEntropyLoss. It scores how much probability your model put on the correct class.
import torch.nn as nn
loss_fn = nn.CrossEntropyLoss()Pass Raw Logits Directly
This is the key rule: feed CrossEntropyLoss the raw logits, never a softmax output. It applies log-softmax internally for you.
loss = loss_fn(logits, labels)Why Not Softmax First
Applying softmax yourself, then this loss, squashes the values twice. That double pass distorts gradients and quietly wrecks training.
Labels Are Class Indices
Your targets are plain integer indices, like 3 for class three, not one-hot vectors. PyTorch handles the lookup for you.
labels = torch.tensor([3, 0, 7])Mind the Output Shape
Logits have shape batch by classes, while labels have shape batch only. A shape mismatch here is the most common cross-entropy error.
It Is Built From Two Pieces
Under the hood, CrossEntropyLoss equals log-softmax followed by negative log-likelihood. Fusing them keeps the math numerically stable.
Predict With Argmax
To get the final answer, take the argmax over the logits. The largest score is the predicted class, no softmax needed.
preds = logits.argmax(dim=1)Smooth Labels for Generalization
Setting label_smoothing nudges targets slightly away from a hard 1, which discourages overconfidence and often improves test accuracy.
loss_fn = nn.CrossEntropyLoss(label_smoothing=0.1)Quick Check
You are using nn.CrossEntropyLoss for 10 classes. What should the model's final output be?
Recap: One Winner Per Sample
For multiclass tasks you feed raw logits and integer labels to CrossEntropyLoss, which softmaxes internally and scores the right class. Argmax gives the prediction. 🏆
الأسئلة الشائعة
هل درس «Cross-Entropy للتصنيف متعدد الفئات» مجاني؟
نعم — نص درس «Cross-Entropy للتصنيف متعدد الفئات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Deep Learning Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Deep Learning Academy 4 دروس في المجموع.
ماذا ستتعلم في «Cross-Entropy للتصنيف متعدد الفئات»؟
لماذا تتوقع Logits خامًا لا Softmax تتمرن على Deep Learning Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Deep Learning Academy؟
لا تُشترط خبرة سابقة. Deep Learning Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «Cross-Entropy للتصنيف متعدد الفئات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Deep Learning Academy هذا؟
نعم. كل درس في Deep Learning Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- MSE وMAE للانحدار
- Binary Cross-Entropy مع Logits
- Cross-Entropy للتصنيف متعدد الفئات
- أوزان الفئات للبيانات غير المتوازنة