Why is the pytorch Loss base class protected?

johann-petrak: <p>See <a href="https://pytorch.org/docs/stable/_modules/torch/nn/modules/loss.html" class="inline-onebox" rel="noopener nofollow ugc">torch.nn.modules.loss — PyTorch 1.8.1 documentation</a> where classes <code>_Loss</code> and <code>_WeightedLoss</code> are protected and not getting imported in <code>__init__</code>.</p>
<p>What is the reason for this? For users who want to their own loss class it would be useful to both make use of the existing base classes and use them to better document the purpose of their own implementation as identifying them e.g. as a weighted loss.</p>

tom: <p>The main reason for having the <code>_Loss</code> class is for the backward-compatibility of the reduction method.<br>
There is nothing special about a loss compared to other <code>nn.Module</code> classes and I personally would think that it is good that no-one gets the idea of doing <code>isinstance(l, Loss)</code> anywhere because that would preclude passing in a <code>nn.Module</code> that does some processing and then calls loss.</p>
<p>Best regards</p>
<p>Thomas</p>

johann-petrak: <p>I really cannot follow that argument - the ability to subclass opens the possibility to re-use code already there and is normally not something where checking with isinstance is a concern. ALL concrete implementations of loss classes can be subclassed anyways and the same goes for other module classes, but making e.g. _WeightedLoss protected just means that nobody can easily re-use the (reduction) code in that base class. My question was: why would that be a bad thing?<br>
Not allowing it forces people to copy/paste the code or reimplement something comparable.</p>

tom: <aside class="quote no-group" data-username="johann-petrak" data-post="3" data-topic="123417">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/johann-petrak/48/44372_2.png" class="avatar"> johann-petrak:</div>
<blockquote>
<p>nobody can easily re-use the (reduction) code in that base class. My question was: why would that be a bad thing?</p>
</blockquote>
</aside>
<p>The idea here is that for any reasonably modern code, the re-usable part would be exactly <code>self.reduction = reduction</code>.<br>
What I am trying to suggest is exactly this: New code should not provide the deprecated arguments <code>size_average</code> and <code>reduction</code>, so re-using the translation of them to then set <code>self.reduction = </code> … should not be terribly interesting.<br>
When PyTorch finally drops the support for the deprecated arguments, <code>_Loss</code> might go away. (Yeah, and I readily admit that there is no reason why <code>CTCLoss</code> should subclass <code>_Loss</code> when adopting this line of reasoning.)</p>
<p>But maybe I am not understanding this right. Given the current constraints, what would you be copy-pasting to make your code work better?</p>
<p>Best regards</p>
<p>Thomas</p>

johann-petrak: <p>Sorry, my mistake, I misunderstood what the legacy reduction issue is all about.<br>
Totally makes sense to me now!</p>