@ptrblck thanks for the explanation. I tried replacing

term_b = torch.log(1.0 - predicted)

with

term_b = torch.clamp(term_b,-100.0,0.0)

and using your code to double check, it seems like it will still produce nan. In this case should I add a small 1e-8 inside the log term? but that would produce some difference with the value calculated with nn.BCELoss