'PyTorch: Use BCELoss for multi-label, binary classification problem
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?
Solution 1:[1]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mai |
