This might sound a little basic but while running the code below, I wanted to see the source code of the backward function:

import torch.nn as nn

criterion = nn.CrossEntropyLoss()
loss = criterion(output, target)

So I went to the PyTorch GitHub and found the CrossEntropyLoss class, but without any backward function defined. Moving up, CrossEntropyLoss extends _WeightedLoss >> _Loss >> Module then still nothing.

So, where is the backward function defined?


1 Answer 1


Backward function is same for all type of layers.

Look at 155 number line here.


and it will forward you to here


  • 1
    $\begingroup$ criterion return a Tensor... oh... i missed that part. Thanks a lot! $\endgroup$
    – Begoodpy
    Jul 8, 2020 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.