Simple DNN dimension problem

criterion = nn.CrossEntropyLoss()
for i in range(10):
_, output = torch.max(model(x_train_tensor[i]), 0)
print(output, y_train_tensor[i])
print(criterion(output, y_train_tensor[i]))

output is:
tensor(0), tensor(0)
error: RuntimeError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

how can i fix it?

Maybe you misunderstood about nn.CrossEntropyLoss()?
You need not take max and pass the index to the loss.
nn.CrossEntropyLoss() expects a 2D array of logits where each row belongs to values predicted for a particular item.

1 Like

if i need classify two classes[ex. true, false], which loss function should be used? can you suggest for me?

You can use either nn.BCELoss() or nn.CrossEntropyLoss().

For nn.BCELoss(), your network shall have one output node with Sigmoid output activation.
For nn.CrossEntropyLoss(), your network shall have two output node without any activation.

1 Like

if my DNN model’s last layer use Relu activation function, BCELoss function can not use?

I think, you should change the final node’s activation to Sigmoid if you are planning to use nn.BCELoss().
Because, 0 <= nn.ReLU(output) <= inf and BCELoss expects probability (0 to 1).