Binary classification - BCELoss and model output size not corresponding
I'm doing a binary classification, hence I used a binary cross entropy loss:
criterion = torch.nn.BCELoss()

However, I'm getting an error:
Using a target size (torch.Size([64, 1])) that is different to the input size (torch.Size([64, 2])) is deprecated. Please ensure they have the same size.

My model ends with:
    x = self.wave_block6(x)
    x = self.sigmoid(self.fc(x))
    return x.squeeze()

I tried removing the squeeze, but to no avail. My batch size is 64. It seems like I'm doing something simple wrong here. Is my model giving 1 output and BCE loss expecting 2 inputs? Which loss should I use then?

Binary Cross-Entropy Loss  (BCELoss) is used for binary classification tasks. Therefore if N is your batch size, your model output should be of shape [64, 1] and your labels must be of shape [64].Therefore just squeeze your output at the 2nd dimension and pass it to the loss function -
Here is a minimal working example
import torch
a = torch.randn((64, 1))
b = torch.randn((64))
loss = torch.nn.BCELoss()

b = torch.round(torch.sigmoid(b)) # just to create some labels
a = torch.sigmoid(a).squeeze(1)
l = loss(a, b)

Update - Basing on the conversation in the comments, focal loss can be defined as follows -
class focalLoss(nn.Module):
    def __init__(self, alpha=0.25, gamma=3):
        super(focalLoss, self).__init__()
        self.alpha = alpha
        self.gamma = gamma

    def forward(self, pred_logits: torch.Tensor, target: torch.Tensor):
        batch_size = pred_logits.shape[0]
        pred = pred.view(batch_size, -1)
        target = target.view(batch_size, -1)
        pred = pred_logits.sigmoid()
        ce = F.binary_cross_entropy(pred_logits, target, reduction='none')
        alpha = target * self.alpha + (1. - target) * (1. - self.alpha)
        pt = torch.where(target == 1, pred, 1 - pred)
        return alpha * (1. - pt) ** self.gamma * ce

