An analog of weighted_cross_entropy_with_logits in PyTorch
I'm trying to train a model with PyTorch. Is there any simple way to create a loss like weighted_cross_entropy_with_logits from Tensorflow?

There are pos_weight argument in weighted_cross_entropy_with_logits that can help with balancing. But there are only weights for labels in the list of arguments in BCEWithLogitsLoss.

You can write your own custom loss function as you want. For example, you can write:

def weighted_cross_entropy_with_logits(logits, target, pos_weight):
    return targets * -logits.sigmoid().log() * pos_weight + 
               (1 - targets) * -(1 - logits.sigmoid()).log()


This is a basic implementation. You should follow the steps mentioned here to ensure stability and avoid overflow. Just use the final formulation that they derived.
