Or you can use https://pytorch.org/docs/stable/nn.functional.html#binary-cross-entropy-with-logits 3

did not help, loss function returened a nan
tensor(nan, device='cuda:0', grad_fn=<BinaryCrossEntropyWithLogitsBackward>)