I am trying to access specific values of PyTorch's Cross-Entropy loss function (torch.nn.functional.cross_entropy) that I believe are being calculated when the input is a vector of length N. I would like access to the vector of N individual loss values; not the mean or sum or whathaveyou as I believe is what is being returned.
From looking at the documentation, I tried setting "reduction = None"; however, it still returns a scalar value. The function's default setting is to return the mean.
Here is the error message:
Exception has occurred: IndexError
slice() cannot be applied to a 0-dim tensor.
IndexError: slice() cannot be applied to a 0-dim tensor.
Here's is the code snippet leading up to the error:
tMinusOne_loss = burnIn_model.loss(combined_tMinusOne_X, combined_tMinusOne_Y)
print("tMinusOne_loss:", tMinusOne_loss)
tMinusOne_first_loss = tMinusOne_loss[ :len(combined_tMinusOne_X_first)]
Here is what is printed out from the print line right before the error occurs:
tMinusOne_loss: tensor(0.3171, grad_fn=<NllLossBackward0>)
Thank you!
If I'm understanding the problem correctly, you are computing the cross entropy loss of a vector of size
(N)or(1, N)(ie a single item, not a batch). In this case, there is only one expected loss value.CrossEntropyLossproduces one value per item in the batch.Cross entropy is computed as the negative log prob of the expected class. Since there is only one target class per item, there is only one loss value per item.