Hello lzh21cen!



 lzh21cen:

If I define loss function in another way which will be inherited from nn.Module, must I use torch functions in all forward steps?


You could try it out and see.  Pass a requires_grad = True tensor
through your loss function and see if .backward() gives the correct
result.  Something like:
input = torch.randn ((2, 5), requires_grad=True)
target = ...
loss = my_custom_loss_function (input, target)
loss.backward()
print (input.grad)



        # forward code here

        if self.reduce:
            return torch.mean(F_loss)
        else:
            return F_loss



(As a side note, I have no idea what F_loss means here, so I have
no idea whether this code makes sense or would work the way you
want.)
Best.
K. Frank