mMagmer:

m(input[:,1]-input[:,0])


Unfortunately that BCELoss didn’t work, even BCELossWithLogitsLoss doesn’t work. Do you know how I can fix it?
class Classifier(nn.Module):
    
    def __init__(self, n_class, batch_size):
        super(Classifier, self).__init__()
        self.batch_size = batch_size
        self.transformer = VisionTransformer()
        #self.criterion = nn.CrossEntropyLoss(reduce=False)
        #self.criterion = nn.BCELoss(reduce=False)
        self.criterion = nn.BCEWithLogitsLoss(reduce=False)



    def forward(self, X, labels):

        stacked_X = torch.stack(X)
        out = self.transformer(stacked_X)
       
        with torch.autocast('cuda'):
            # https://discuss.pytorch.org/t/unclear-about-weighted-bce-loss/21486/2 
            labels = torch.tensor(labels)
            weight = torch.tensor([0.1, 0.9]) # how to decide on this weights?
            weight_ = weight[labels.data.view(-1).long()].view_as(labels)
            m = nn.Sigmoid()
            print('sig: ', m(out[:,1]-out[:,0]))
            loss = self.criterion(torch.cuda.LongTensor(m(out[:,1]-out[:,0])), torch.tensor(labels).cuda())
            print('loss: ', loss)
            print('weight_: ', weight_)
            loss_class_weighted = loss * weight_.cuda()
            loss_class_weighted = loss_class_weighted.mean()
       
       
        #pred = out.data.max(1)[1]
        
        pred_labels = out.argmax(dim=1)
        
        return pred_labels, labels, loss_class_weighted

The error is:
        [ 0.2062, -0.2541],
        [-0.1909,  0.0930],
        [-0.1987, -0.3082],
        [-0.1557,  0.1971],
        [-0.0801, -0.0162],
        [ 0.3868,  0.2435],
        [ 0.7702,  0.0296],
        [ 0.1791, -0.1098],
        [-0.2040,  0.0221],
        [ 0.1514, -0.0552],
        [-0.0038, -0.0221],
        [-0.1212,  0.2830],
        [-0.1849, -0.3254],
        [ 0.0826, -0.2480],
        [ 0.1392, -0.2806]], device='cuda:0', grad_fn=<AddmmBackward0>)
sig:  tensor([0.4937, 0.3869, 0.5705, 0.4727, 0.5873, 0.5160, 0.4642, 0.3229, 0.4283,
        0.5563, 0.4485, 0.4954, 0.5997, 0.4649, 0.4181, 0.3966],
       device='cuda:0', grad_fn=<SigmoidBackward0>)
Traceback (most recent call last):
  File "main_classifier.py", line 250, in <module>
    pred,label,loss = trainer.train(sample_batched, model)
  
    pred, labels, loss = model.forward(feats, labels)

    return self.module(*inputs[0], **kwargs[0])
  File "/home/jalal/research/venv/dpcc/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
    return forward_call(*input, **kwargs)
 
    loss = self.criterion(torch.cuda.LongTensor(m(out[:,1]-out[:,0])), torch.tensor(labels).cuda())
TypeError: expected TensorOptions(dtype=long int, device=cuda, layout=Strided, requires_grad=false (default), pinned_memory=false (default), memory_format=(nullopt)) (got TensorOptions(dtype=float, device=cuda:0, layout=Strided, requires_grad=false (default), pinned_memory=false (default), memory_format=(nullopt)))