0Pricing
Deep Learning Academy · Lezione

Cross-entropy per più classi

Scopra perché richiede logits grezzi, non softmax

Cross-entropy per più classi è una lezione Deep Learning Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Deep Learning Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Deep Learning Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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. 🏆

Domande Frequenti

La lezione «Cross-entropy per più classi» è gratuita?

Sì — il testo completo di «Cross-entropy per più classi» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Deep Learning Academy, passa a CoddyKit PRO. Il corso Deep Learning Academy include 4 lezioni in totale.

Cosa imparerò in «Cross-entropy per più classi»?

Scopra perché richiede logits grezzi, non softmax Eserciti Deep Learning Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Deep Learning Academy?

Non è richiesta alcuna esperienza precedente. Deep Learning Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.

Quanto tempo richiede la lezione «Cross-entropy per più classi»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Deep Learning Academy?

Sì. Ogni lezione Deep Learning Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. MSE e MAE per la regressione
  2. Binary cross-entropy con logits
  3. Cross-entropy per più classi
  4. Pesi delle classi per dati sbilanciati
← Torna a Deep Learning Academy