Usage of cross entropy loss

Hi, everyone!

About the usage of Cross Entropy Loss in PyTorch…
Someone could make an example illustrating how to use this loss?
I am getting this error(Dimension out of range (expected to be in range of [-1, 0], but got 1)), I am already saw the other posts about it but I didn’t understand how to use this loss yet.

Best regards,

Matheus Santos.

Please see if this helps.

input = torch.randn(4,4)
target = torch.randint(1,4,(4,))
loss = F.cross_entropy(input, target)

Please note that “input” is a 2d tensor whereas “target” is 1d tensor. The function expects first input tensor to be 2 dimensional, (N,C) and second tensor to be 1 dimensional. See [doc]

2 Likes

I see!! I understood how it works!!
Thanks for the reply and help!! :smiley:

This really confused me. I put together a quick blog post in case it helps anyone else: https://jbencook.com/cross-entropy-loss-in-pytorch/

1 Like

Thank you for the nice recommendation.
I have a question in regards to tuning the thresholding to favor some classes. Could you elaborate on what you mean by this and how we can achieve the tuning?

You could e.g. plot the ROC curve of the model predictions and pick a threshold based on the returned stats.