I’m new to Pytorch, i’m having trouble properly implementing custom loss function that will be compatible with backward() pass. For some trivial experiments it’s ok, for others i’m getting a “element 0 of tensors does not require grad and does not have a grad_fn” error.
I did check requires_grad on arguments for following examples, x True, y False, p False.
class cu_loss(torch.nn.Module):
def init(self):
super(cu_loss,self).init()
#1  def forward(self,x,y):
da = y - torch.argmax(x, dim=1)
result = {…}
return result
#2  def forward(self,x,y,p):
diff = torch.sum(y-p)
return diff
#1 forward fails simply after addition of torch.argmax function
#2 forward i have no idea, am i not allowed to simply substract tensors with minus sign? This seems to be a common problem.
I read from similar topics that i can’t use functions outside of pytorch, because some data can escape the internal state of the graph? Is there a resource with general pointers how to properly do that?