In order to ensure that I understood how BCE with logits loss works in pytorch, I tried to manually calculate the loss, however I cannot reconcile my manual calculation with the loss generated by the pytorch function F.binary_cross_entropy_with_logits.
can somebody please explain what i am doing wrong.  My code is below:
import torch
import torch.nn.functional as F
import math
def sigmoid(x):
return 1/(1+math.exp(-x))
def manual_cross_entropy_with_logits(inp, target):
y=target[0]
x=inp[0]
p1=y*(math.log(sigmoid(x),2))
p0=(1-y)*math.log(1-sigmoid(x),2)
return -1 * (p1 + p0)

inp = torch.randn(1, requires_grad=True)
target = torch.empty(1).random_(2)
loss = F.binary_cross_entropy_with_logits(inp, target)
lossman=manual_cross_entropy_with_logits(inp,target)
print(‘bce loss’)
print(loss)
print(‘manual loss’)
print(lossman)