Multi-Label, Multi-Class class imbalance

I think the easiest approach would be to specify reduction='none' in your criterion and then multiply each output with your weights:

target = torch.tensor([[0,1,0,1,0,0]], dtype=torch.float32)
output = torch.randn(1, 6, requires_grad=True)
weights = torch.tensor([0.16, 0.16, 0.25, 0.25, 0.083, 0.083])

criterion = nn.BCEWithLogitsLoss(reduction='none')
loss = criterion(output, target)
loss = (loss * weights).mean()
loss.backward()

Would that work for you?

16 Likes