Is there a way to write a custom BCE loss in pytorch?
I am writing a custom BCE in pytorch but in some cases it returns -inf and nan most cases. Which is due to the log function.

bce_loss=y_true*torch.log2(y_pred) +(one_torch-y_true)*torch.log2(one_torch-y_pred)


Is there a way to rewrite this? Note y_pred is a sigmoid output which is between 0 and 1.

You can clamp the preds to stop from log error.

y_pred = torch.clamp(y_pred, 1e-7, 1 - 1e-7)
