How can i train a neural network in pytorch wiht some self-defined loss function

If i have defined a traditional CNN network with builtin CrossENtropy function, then i can compute a loss and backward with the pytorch API : loss.backwar().

But i defined some new loss function based on the original loss, like this ** NewLoss = OriginalLoss + lambda*something**, how do i backward with this new loss function? Or anyone can provide a github project which train a neural network with self-defined loss function?

Thanks in advance for help

NewLoss.backward() will do.

I have try this, but it does not work. Have you ever read some code for this problem?

Do you have a small code snippet?

if your new loss function is written in terms of variable methods it should just work; otherwise read this: http://pytorch.org/docs/master/notes/extending.html

this blog may be of help
https://spandan-madan.github.io/A-Collection-of-important-tasks-in-pytorch/