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

using nn.Moudule
Build your own loss function in PyTorch 8.6k
Write Custom Loss Function 4.8k


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)

  
    
    
    [Solved] What is the correct way to implement custom loss function? 
  
  
    I tried to implement my own custom loss based on the tutorial in extending autograd. 
Here is the implementation outline: 
class MyCustomLoss(Function):

    def forward(self, input, target):
        ... # implementation
        return loss   # a single number (averaged loss over batch samples)

    def backward(self, grad_output):
        ... # implementation
       return grad_input

The forward function take an input from the previous layer and target which contains array of labels (categoric…
  


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