マルチクラス分類のクロスエントロピー
softmaxではなく生のロジットを受け取る理由を学びます
「マルチクラス分類のクロスエントロピー」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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. 🏆
よくある質問
「マルチクラス分類のクロスエントロピー」レッスンは無料ですか?
はい。「マルチクラス分類のクロスエントロピー」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。
「マルチクラス分類のクロスエントロピー」で何を学びますか?
softmaxではなく生のロジットを受け取る理由を学びます ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Deep Learning Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「マルチクラス分類のクロスエントロピー」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDeep Learning Academyレッスンでコードを書いて実行できますか?
はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 回帰のためのMSEとMAE
- ロジット付きバイナリクロスエントロピー
- マルチクラス分類のクロスエントロピー
- 不均衡データのためのクラス重み