Should I define my custom loss function as a class?

I need to implement a custom loss function and looking for best practices on doing so. In the PyTorch documentation, it is clear that the predefined loss functions are implemented as classes. My question is, should I also define my custom loss function as a class or a normal Python function would do? I also would like to know why PyTorch implements losses as classes. Thanks in advance!

It’s not really necessary.
It’s all about how pytorch is structured. Classes calls in the enf functionals which are functions with no kwargs but only args whose signature is c++.

So they just follow that convention.

Another reason is that some class may have weighting schemes and if they are defined as classes (nn.Modules) they can be allocated on cuda easily and recursively.

1 Like