0Pricing
Deep Learning Academy · บทเรียน

Cross-Entropy สำหรับหลายคลาส

เหตุใดจึงคาดหวัง logits ดิบ ไม่ใช่ softmax

Cross-Entropy สำหรับหลายคลาส เป็นบทเรียน Deep Learning Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 สำหรับหลายคลาส” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Deep Learning Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “Cross-Entropy สำหรับหลายคลาส”

เหตุใดจึงคาดหวัง logits ดิบ ไม่ใช่ softmax คุณปฏิบัติ Deep Learning Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Deep Learning Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Deep Learning Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “Cross-Entropy สำหรับหลายคลาส” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Deep Learning Academy นี้ได้ไหม

ได้ บทเรียน Deep Learning Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. MSE และ MAE สำหรับการถดถอย
  2. Binary Cross-Entropy พร้อม Logits
  3. Cross-Entropy สำหรับหลายคลาส
  4. น้ำหนักคลาสสำหรับข้อมูลไม่สมดุล
← กลับไปที่ Deep Learning Academy