Bobot Kelas untuk Data Tidak Seimbang
Cegah kelas mayoritas mendominasi
Bobot Kelas untuk Data Tidak Seimbang adalah pelajaran Deep Learning Academy gratis di CoddyKit. Ini adalah pelajaran 4 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.
When One Class Dominates
If 95 percent of your samples are one class, the model can score high by always guessing it. That is the class imbalance trap. ⚖️
Why Accuracy Lies Here
A lazy model that ignores the rare class still looks 95 percent accurate. So accuracy hides total failure on the minority you actually care about.
The Loss Follows the Majority
Each sample contributes equally to the loss, so the abundant class pulls the gradient hardest. The model learns mostly from the majority class.
Reweight the Loss
The fix is to give rare classes a heavier voice. Class weights multiply each sample's loss so minority mistakes count for more.
Bigger Weight for Rarer Class
A common recipe sets each weight inversely proportional to its class frequency. The rarer the class, the larger its weight grows.
Pass Weights to CrossEntropy
For multiclass tasks, hand a weight tensor of length C to the loss. Each entry scales the penalty for that class's errors.
import torch.nn as nn
w = torch.tensor([1.0, 9.0])
loss_fn = nn.CrossEntropyLoss(weight=w)pos_weight for Binary Tasks
For two-class problems, BCEWithLogitsLoss takes pos_weight, a single number that boosts the positive class to offset its rarity.
loss_fn = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([9.0]))Sample, Don't Just Weight
Weighting is not the only tool. You can also oversample the rare class or undersample the common one to balance each batch.
Balance Batches With a Sampler
A WeightedRandomSampler draws minority samples more often, so every batch the DataLoader serves is roughly balanced by design.
from torch.utils.data import WeightedRandomSamplerFocal Loss for Hard Cases
For extreme imbalance, focal loss down-weights easy, well-classified examples so the model focuses on the hard, rare ones.
Judge by the Right Metric
After rebalancing, drop accuracy and track F1 or precision and recall. They reveal whether the rare class is finally being learned.
Quick Check
Your dataset is 95 percent negative. How do you stop the model from ignoring positives?
Recap: Give the Rare Class a Voice
Imbalance lets the majority hijack training, so you fight back with class weights, balanced sampling, or focal loss, and you judge results with F1. 💪
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Bobot Kelas untuk Data Tidak Seimbang” gratis?
Ya — teks lengkap “Bobot Kelas untuk Data Tidak Seimbang” 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 “Bobot Kelas untuk Data Tidak Seimbang”?
Cegah kelas mayoritas mendominasi 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 4 dari 4.
Berapa lama pelajaran “Bobot Kelas untuk Data Tidak Seimbang” 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