Custom loss function in pytorch 1.10.1
I am struggeling with defining a custom loss function for pytorch 1.10.1. My model outputs a float ranging from -1 to +1. The target values are floats of arbitrary range. The loss should be a sum of pruducts if the sign between the model output and target is different.
I have searched the internet for quite some hours, but it seems there have been some changes to pytorch throughout the last versions, so I don't really know which example would best fit to my use case and pytorch 1.10.1.
Here is my approach so far:
class Loss(torch.nn.Module):
    @staticmethod
    def forward(self, output, target) -&gt; Tensor:
        loss = 0.0
        
        for i in range(len(target)):
            o = output[i,0]
            t = target[i]
            l = o * t
            if l&lt;0:   #if different sign
                loss -= l

        return loss

Question:

Should I subclass torch.nn.Module or torch.autograd.Function?

Do I need to define @staticmethod?

On some examples, I saw ctx instead of self being used and invocations of ctx.save_for_backward etc. Do I need this? What is its purpose?

When subclassing torch.nn.Module, my code complains: 'Tensor' object has no attribute 'children'. What am I missing?

When subclassing torch.autograd.Function, my code complains about not having a backward function defined. How should my backward function look like?



Custom loss functions can be as simple as a python function. You can simplify this a bit:
def custom_loss(output, target):
    prod = output[:,0]*target
    return -prod[prod&lt;0].sum()
 

