I think the issue is that you have 2 means

The first one is that you have the sum and then divide by the size (but you should use target.numel() instead of target.size(1))
Second is when you use torch.mean

So 2 options:
Here is your code with torch.mean
def bce_loss(pred, target):
    pred=F.sigmoid(pred)
    loss=torch.mean(-(target * torch.log(pred) + (1-target) * torch.log(1-pred)))
    return loss

And here is your code with manual mean (sum and divide)
def bce_loss(pred, target):
    pred=F.sigmoid(pred)
    loss=torch.sum(-(target * torch.log(pred) + (1-target) * torch.log(1-pred))) / target.numel()
    return loss

That said, they say that using the official torch.nn.BCEWithLogitsLoss() is better, cause although it’s doing sigmoid and then BCE, it does it in such a way that it is numerically stable (i believe the optimization is on the c++ level)
Roy.