title: Custom loss function in pytorch 1.10.1
body: <p>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.</p>
  <p>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.</p>
  <p>Here is my approach so far:</p>
  <pre><code>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
  </code></pre>
  <p>Question:</p>
  <ol>
  <li><p>Should I subclass <code>torch.nn.Module</code> or <code>torch.autograd.Function</code>?</p>
  </li>
  <li><p>Do I need to define <code>@staticmethod</code>?</p>
  </li>
  <li><p>On some examples, I saw <code>ctx</code> instead of <code>self</code> being used and invocations of <code>ctx.save_for_backward</code> etc. Do I need this? What is its purpose?</p>
  </li>
  <li><p>When subclassing <code>torch.nn.Module</code>, my code complains: 'Tensor' object has no attribute 'children'. What am I missing?</p>
  </li>
  <li><p>When subclassing <code>torch.autograd.Function</code>, my code complains about not having a backward function defined. How should my backward function look like?</p>
  </li>
  </ol>