PyTorch: Use BCELoss for multi-label, binary classification problem

1.5k views Asked by At

I am currently working on a PyTorch model which should solve a multi-label, binary classification problem.

The last layer of my model is a Sigmoid layer and I would like to use BCELoss from Pytorch.

def train_step(self, x, y):
    self._optim.zero_grad()
    output = self._model(x)
    loss = self._crit(output, y)
    loss.backward()
    self._optim.step()

Here, y is e.g. tensor([[0, 0]]) (the two labels as integers), but the output is e.g. tensor([[0.5332, 0.3933]], grad_fn=<SigmoidBackward>).

This causes the error:

{RuntimeError}Expected object of scalar type Float but got scalar type Long for argument #2 'target' in call to _thnn_binary_cross_entropy_forward

Any idea how to fix this?

1

There are 1 answers

0
Mai On

If you have defined a dataset class, you should return the label of the type tensor float using something like this:

"targets": torch.tensor(self.target[item], dtype=torch.float)