Where to find implementation of Cross Entropy loss function


I love the amazing Pytorch docs, but one issue we ran into is that they seem a bit sparse on the topic of how loss functions, specifically Cross Entropy, is implemented (i.e. mathematically and/or the raw code).

Does someone have an insight into where I would go about finding that?

Context: We are implementing a custom loss function that can incorporate class weights in “distribution-based classes” (not quite sure what the specific term is).

(Sorry if this is a out-of-scope question – feel free to refer me to where a question like this would be more appropriate)

A manual (and slow) implementation can be found here and the CPU implementation calls into cross_entropy_loss and then into nll_loss.

1 Like

Thank you, very helpful! Highly appreciated! If I had been a bit more lucky in my search, I should of course have found your reference on my own, but alas.

(Out of topic-context: And just now that I have the chance, also thank you so much for not just this answer but the tons of other answers you provide in here that prove hyper-helpful on a daily basis)

1 Like