Binary Cross-Entropy dengan Logits
Loss stabil untuk masalah dua kelas
Binary Cross-Entropy dengan Logits adalah pelajaran Deep Learning Academy gratis di CoddyKit. Ini adalah pelajaran 2 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.
Two Classes, One Decision
When your model answers a yes or no question like spam or not spam, you reach for binary cross-entropy, the loss built for two-class problems. ✅
From Score to Probability
A model first outputs a raw score called a logit. The sigmoid function squashes that logit into a probability between 0 and 1.
import torch
prob = torch.sigmoid(logit)What Cross-Entropy Rewards
Cross-entropy gives a small loss when the predicted probability is close to the true label, and a large loss when it is confidently wrong.
Confident and Wrong Hurts
Predicting 0.99 when the true label is 0 produces a huge loss. Cross-entropy punishes confident mistakes far more than uncertain ones.
The Naive Loss Choice
You could apply sigmoid yourself, then use nn.BCELoss on the probabilities. It works, but there is a safer way for real training.
import torch.nn as nn
loss_fn = nn.BCELoss()
loss = loss_fn(prob, target)Feed Logits, Not Probabilities
The preferred loss is BCEWithLogitsLoss. It takes raw logits directly and applies the sigmoid internally for you.
import torch.nn as nn
loss_fn = nn.BCEWithLogitsLoss()
loss = loss_fn(logit, target)Why Combine the Steps
Fusing sigmoid and the loss uses the log-sum-exp trick, which avoids overflow and gives stable gradients even at extreme logits.
Skip the Extra Sigmoid
Because BCEWithLogitsLoss applies sigmoid itself, your model's last layer should output raw logits with no sigmoid attached.
Targets Are 0 or 1
Pass float targets of 0.0 or 1.0 with the same shape as your logits. A mismatched shape is the classic BCE bug to watch for.
target = torch.tensor([1.0, 0.0, 1.0])Sigmoid Only at Inference
During training the loss handles the squashing. To read a probability for a prediction, apply sigmoid to the logit at inference time.
Tilt the Balance with pos_weight
If positives are rare, the pos_weight argument scales up their contribution so the model stops ignoring the minority class.
Quick Check
You are doing binary classification and want numerical stability. What should you feed the loss?
Recap: Stable Yes or No
For two-class tasks you feed raw logits to BCEWithLogitsLoss, which squashes and scores in one stable step. Save sigmoid for reading probabilities later. 👍
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Binary Cross-Entropy dengan Logits” gratis?
Ya — teks lengkap “Binary Cross-Entropy dengan Logits” 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 “Binary Cross-Entropy dengan Logits”?
Loss stabil untuk masalah dua kelas 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 2 dari 4.
Berapa lama pelajaran “Binary Cross-Entropy dengan Logits” 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
- MSE dan MAE untuk Regresi
- Binary Cross-Entropy dengan Logits
- Cross-Entropy untuk Multikelas
- Bobot Kelas untuk Data Tidak Seimbang