Class weights assign relative importance to target classes during training. They address class imbalance by penalizing mistakes on rare classes more heavily, helping fraud or forgery models pay attention to minority examples. section 3.4.2 section 6.3.2
Use class weights as a tunable part of the training workflow:
- Inspect class frequencies before training so the imbalance and minority class are explicit. section 3.9
- Choose uniform weights, inverse-frequency balancing, or an explicit ratio such as {0: 1, 1: 2}. section 3.4.2
- Apply the weights to the loss function, increasing the cost of mistakes on the selected class. section 3.4.2 section 6.3.2
- Tune the values with validation metrics such as AUPRC, because excessive weighting can cause the model to predict the minority class too often. section 3.4.2 section 6.3.2
Fraud and receipt-forgery settings
For a fraud-to-genuine ratio of 1:100, balanced weighting assigns approximately {0: 1, 1: 100}, while uniform weighting uses {0: 1, 1: 1}. A random forest search selected {0: 1, 1: 2}; a segmentation example used frequencies [1.2, 1] to derive inverse weights for forged and genuine pixels. section 3.4.2 section 6.3.2
class_frequencies = [1.2, 1] #A weights = 1.0 / torch.tensor(class_frequencies, dtype=torch.float32) #B optimizer = torch.optim.Adam(model.parameters(), lr=1e-4) #C loss_function = torch.nn.CrossEntropyLoss(weight=weights) #C
Shows inverse-frequency weights passed directly to PyTorch CrossEntropyLoss for segmentation training. section 6.3.2
