How does class-weighted cross-entropy adjust gradient contributions for rare classes?
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
How does class-weighted cross-entropy adjust gradient contributions for rare classes? Describe the effect on a single example's gradient, on the gradient summed over a dataset, and on the trained model.
```hint Differentiate it
Write the weighted per-example loss, take its gradient with respect to the logits, and compare it with the gradient of ordinary softmax cross-entropy.
```
```hint Sum over the dataset
Group the summed gradient by class. How much does each class contribute when the weights scale with the inverse of class frequency?
```
### What a Strong Answer Covers
- The weighted per-example loss, its gradient with respect to the logits, and what the weight changes about each update.
- How the weights change each class's share of the total gradient under a common weighting scheme.
- The relation between class weighting and oversampling.
- The side effects: decision boundary shift, precision and recall, gradient variance, overfitting, and gentler weighting schemes.
- How the loss is normalized in practice when weights are combined with mean reduction.
### Follow-up Questions
- How do class weights affect the calibration of the predicted probabilities, and how would you correct for it?
- How does focal loss differ from class weighting in which examples it up-weights?
- For a fraud model at 0.1% prevalence, would you choose class weights, oversampling or threshold tuning, and why?
- What happens to weighted training when most mini-batches contain no rare-class examples at all?
Overview: Asks how class-weighted cross-entropy changes the gradient each example contributes and why rare-class examples end up pushing the parameters harder. Tests the logit gradient derivation, inverse-frequency weighting, the link to oversampling, and the side effects on recall, precision and gradient variance.