I would like to do binary classification with softmax in Pytorch. Even though I set the number of output as 2 and use “nn.CrossEntropyLoss()”, I am getting the following error:

RuntimeError: 0D or 1D target tensor expected, multi-target not supported

train_loss = []
for epoch in range(epochs):
y_pred = model(x_train)
loss = loss_func(y_pred, y_tensor)
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss.append(loss.item())

The size I am getting from y_pred and y_tensor is torch.Size([3000, 2]) torch.Size([3000, 1]). How this issue can be solved?

Yes, please change 4 to 1 because the last dimension of your prediction output is 2
You can achieve it by y_tensor[y_tensor == 4] = 1 (or modify it in the dataloader)