[Solved] What is the correct way to implement custom loss function?

can i confirm that there are two ways to write customized loss function:

  1. using nn.Moudule
    Build your own loss function in PyTorch - #18 by edgarriba
    Write Custom Loss Function - #2 by fmassa

Here you need to write functions for init() and forward().
backward is not requied. But how do I indicate that the target does not need to compute gradient?

2)using Functional (this post)

Here you need to write functions for both forward() and backward()

10 Likes