@albanD No I am doing like this.
criterion = nn.BCELoss() 
pred = torch.sigmoid(pred)
loss = criterion(pred, target)

It still giving error, but if I add clamp the error resolved.
criterion = nn.BCELoss() 
pred = torch.clamp(torch.sigmoid(pred),0,1)
loss = criterion(pred, target)

Which means the output of sigmoid is not in range 0 and 1, or maybe because of the precision problem. However suppose if I implement the attention module, which use sigmoid to produce [0-1] range, it will has problem because maybe the result is not pure 0 or 1 in range.