0Pricing
Deep Learning Academy · Pelajaran

Cross-Entropy untuk Multikelas

Mengapa yang diharapkan adalah logits mentah, bukan softmax

Cross-Entropy untuk Multikelas adalah pelajaran Deep Learning Academy gratis di CoddyKit. Ini adalah pelajaran 3 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Deep Learning Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Deep Learning Academy mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Cross-Entropy untuk Multikelas” gratis?

Ya — teks lengkap “Cross-Entropy untuk Multikelas” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Deep Learning Academy, upgrade ke CoddyKit PRO. Kursus Deep Learning Academy mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Cross-Entropy untuk Multikelas”?

Mengapa yang diharapkan adalah logits mentah, bukan softmax Kamu berlatih Deep Learning Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Deep Learning Academy?

Tidak diperlukan pengalaman sebelumnya. Deep Learning Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 3 dari 4.

Berapa lama pelajaran “Cross-Entropy untuk Multikelas” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Deep Learning Academy ini?

Ya. Setiap pelajaran Deep Learning Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. MSE dan MAE untuk Regresi
  2. Binary Cross-Entropy dengan Logits
  3. Cross-Entropy untuk Multikelas
  4. Bobot Kelas untuk Data Tidak Seimbang
← Kembali ke Deep Learning Academy