I would generally recommend to use nn.BCEWithLogitsLoss 12 instead of nn.BCELoss for increased numerical stability and the availability of the pos_weight argument.
Note that both loss funcitons are used for a binary of multi-label classification/segmentation, so I’m not sure, if it’ll fit your use case.
That being said, you could specify the pos_weight argument as described in the docs to balance a class imbalance in your dataset. You could also use the weight argument to rescale each sample in the batch. In the latter case, I would personally use the functional API, as you would probably recreate the criterion in each iteration based on the current batch.