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