Hi,
The logic you using is correct, actually the data types are causing this differences.
For instance, if you use your own code in functions:
count_p = (b == 1.0).sum() + 1
count_n = (b == 0.0).sum() + 1                
loss2 = torch.mean(-((b * a.log()) * (1 / count_p)) - (((1 - b) * (1 - a).log()) * (1 / count_n)))

count_p instead of 1 and count_n instead of 10, you will get same result. Note if you add 1. or 10., you will get different result.
So the most secure way to use constant numbers is to use torch.tensor() method.
loss2 = torch.mean(-((b * a.log()) * (1 / torch.tensor([1]).cuda())) - (((1 - b) * (1 - a).log()) * (1 / torch.tensor([10]).cuda())))

torch.tensor() retains the datatype while moves tensor to the given device.
Bests
Nik