What's the difference between module._parameters vs. module.parameters?

kirk86: <p>It seems that modules provide some functionality through functions of the form <code>module.some_function</code> but also <code>module._some_function</code> (i.e. notice the <code>_underscore_</code> notation).</p>
<p>For instance <code>module.parameters() vs. module._parameters</code>.</p>
<p>It seems that <code>module.parameters()</code> returns the parameters and <code>module._parameters</code> returns an <code>OrdereredDict()</code> of the actual parameters.</p>
<p>What the difference between the two and their corresponding use cases?</p>
<p>Is that difference clearly documented in the docs? Searching through the <code>Module</code> documentation I couldn’t find anything about <code>._parameters</code>?</p>

albanD: <p>Hi,</p>
<p>It is a common convention in python to prepend a <code>_foo</code> to a variable name when it is internal and should not be used by the users.</p>
<p>In particular <code>.parameters()</code> is the public API and <code>._parameters</code> is an internal attribute that you should not use (that is why it is not documented as well).</p>

kirk86: <p>Thanks <a class="mention" href="/u/alband">@albanD</a>,</p>
<aside class="quote no-group" data-username="albanD" data-post="2" data-topic="108924">
<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/alband/48/215_2.png" class="avatar"> albanD:</div>
<blockquote>
<p>In particular <code>.parameters()</code> is the public API and <code>._parameters</code> is an internal attribute that you should not use (that is why it is not documented as well).</p>
</blockquote>
</aside>
<p>This makes sense but I’ve seen a lot of code online that uses the non public API and if I’m not mistaken I’ve seen also some pytorch examples uses it as well.</p>
<p>On another note, if you wanted to loop over the params of a module, do some operation and then remove that param, how would you do that with <code>module.parameters</code>?</p>
<p>With <code>module._parameters</code> one could simply use <code>module._parameters.pop(name)</code>. <a href="https://discuss.pytorch.org/t/passing-multiple-arguments-in-module-apply/108857">Here’s an example of that</a>.</p>

albanD: <blockquote>
<p>This makes sense but I’ve seen a lot of code online that uses the non public API and if I’m not mistaken I’ve seen also some pytorch examples uses it as well.</p>
</blockquote>
<p>Can you link such example please?</p>
<blockquote>
<p>On another note, if you wanted to loop over the params of a module, do some operation and then remove that param, how would you do that with <code>module.parameters</code>?</p>
</blockquote>
<p>The thing is that the internal API is not recursive (won’t look for parameters in child Modules). So they will give you very different result if you’re not careful!<br>
The right way to do this is to use <code>named_parameter()</code> that will return the name of the attribute as well as the parameter and you can then <code>delattr(mod, name)</code> if you want to remove it.</p>

kirk86: <p>Thanks <a class="mention" href="/u/alband">@albanD</a> a lot for clarifying this, I wasn’t aware of it.</p>
<aside class="quote no-group" data-username="albanD" data-post="4" data-topic="108924">
<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/alband/48/215_2.png" class="avatar"> albanD:</div>
<blockquote>
<p>So they will give you very different result if you’re not careful!</p>
</blockquote>
</aside>
<p>Scratch that, I confused the pytorch lib with pytorch examples.</p>
<aside class="quote no-group" data-username="albanD" data-post="4" data-topic="108924">
<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/alband/48/215_2.png" class="avatar"> albanD:</div>
<blockquote>
<p>Can you link such example please?</p>
</blockquote>
</aside>