Better definition of loss

Hi,
Is it better to define a custom loss function in the following way
‘’’
class loss(torch.nn.Module):
code for loss
‘’’
or is is better to implement it as a normal function.

Whatever fits to your problem.
Pytorch losses are defined as modules because they may contain weights.

So the way loss is implemented wont affect the speed,right?

Nope. In the end using a nn.Module is a workaround. You will be more or less end up calling the same function than if you code it as a function.

1 Like