Why is the loss superclass protected?

I wanted to write a custom loss function by subclassing the _Loss superclass. At first I didn’t pay much attention to the underscore until the following import failed:

from torch.nn.modules import _Loss

After that I noticed that _Loss is not included in the imports within the __init__.py.

While this can easily be circumvented by importing it directly

from torch.nn.modules.loss import _Loss

I was left wondering what the intentions might be.

Shall custom loss functions not subclass _Loss but rather module directly? If not, why protect and ‘hide’ it?

2 Likes