When one label is scarce, an unweighted average can let a classifier look good by prioritizing the frequent class. Per-class weights make mistakes on selected labels count more without changing which prediction is correct.
Implement weighted_bce_loss(p, y, class_weights): use the same clipped BCE terms as 02-bce-loss, then multiply each sample by the weight selected from its actual class.
p and binary y have matching sample shapes.class_weights provides keys 0 and 1.[1e-12, 1 - 1e-12].y == 1, otherwise class-0 weight.float without loops.Open one at a time. Each gives away a little more than the last.
First create an array of one weight per label; then it broadcasts naturally against one loss per sample.
np.where(y == 1, class_weights[1], class_weights[0]) performs the per-sample lookup.
No results yet
Run your code to check it against the examples, or submit to run every test.