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) -> Tensor:
loss = 0.0
for i in range(len(target)):
o = output[i,0]
t = target[i]
l = o * t
if l<0: #if different sign
loss -= l
return loss
Question:
Should I subclass
torch.nn.Moduleortorch.autograd.Function?Do I need to define
@staticmethod?On some examples, I saw
ctxinstead ofselfbeing used and invocations ofctx.save_for_backwardetc. 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?