Rename nn.Parameter

apozas: <p>Hi all,</p>
<p>I have been trying to obtain the following behavior when defining an own model: In essence, what I would want is to have a <code>Parameter</code> whose name would be <code>weights</code>, so that in <code>model.state_dict()</code> I would see <code>weights</code>. But, for simplicity in coding, I would want to refer to it in the code as model.W, instead of model.weights. I have tried the following:</p>
<pre><code class="lang-python">from torch.nn import Module, Parameter
from torch import randn

class mymodel(Module):
    def __init__(self):
        super(mymodel, self).__init__()
        W = Parameter(randn(5,4))
        self.register_parameter("weights", W)
        self.W = W

model = mymodel()
</code></pre>
<p>With this code, I can call <code>model.W</code>, but also model.weights. Plus, when I call <code>model.state_dict()</code>, then I obtain a list of two parameters, “W” and “weights”.</p>
<p>I wanted to ask, firstly, if the kind of behavior I would desire (being able to call only to <code>model.W</code> and having only “weights” in <code>model.state_dict()</code>, both referring to the same <code>Parameter</code>) is common/good practice, and if so, how to implement it properly.</p>
<p>Thanks in advance!</p>

tom: <aside class="quote no-group" data-username="apozas" data-post="1" data-topic="51500">
<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/apozas/48/2485_2.png" class="avatar"> apozas:</div>
<blockquote>
<p>I wanted to ask, firstly, if the kind of behavior I would desire (being able to call only to <code>model.W</code> and having only “weights” in <code>model.state_dict()</code> , both referring to the same <code>Parameter</code> ) is common/good practice</p>
</blockquote>
</aside>
<p>No! This is decidedly <em>not</em> good style. You are trading conceptual simplicity for saving a few characters (6 instead of 12).</p>
<p>Now, what you can do if your forward uses <code>self.weights</code> so often that you don’t want to spell it?<br>
I think a perfectly good solution is to do a local assignment <code>W = self.weights</code> at the beginning of the forward (or whatever method uses W). If you use it often enough, it’ll even be less characters.</p>
<p>Best regards</p>
<p>Thomas</p>

apozas: <p>I see. I was kind of expecting this would be the situation. I was looking for a simple way of being a bit more explicit on what <code>W</code> meant in a big code that I had already developed, but probably the simplest thing to do in terms of both explainability of the code and simplicity of the change is just renaming <code>W</code>to <code>weights</code>everywhere. Thank you very much for the clarification.</p>

Ruan_yi: <h2><a name="actually-i-have-the-same-demand-as-i-need-to-generate-models-in-python-and-load-my-model-in-c-and-training-in-c-i-need-to-set-parameter-names-dynamically-in-python-and-get-all-parameter-names-in-c-but-if-i-do-register-then-i-got-different-names-point-to-same-parameter-how-ever-i-found-out-that-python-code-will-only-set-parameter-name-automatically-if-that-parameter-is-direct-class-member-so-my-ugly-solution-is-use-a-dict-to-cache-parameters-1" class="anchor" href="#actually-i-have-the-same-demand-as-i-need-to-generate-models-in-python-and-load-my-model-in-c-and-training-in-c-i-need-to-set-parameter-names-dynamically-in-python-and-get-all-parameter-names-in-c-but-if-i-do-register-then-i-got-different-names-point-to-same-parameter-how-ever-i-found-out-that-python-code-will-only-set-parameter-name-automatically-if-that-parameter-is-direct-class-member-so-my-ugly-solution-is-use-a-dict-to-cache-parameters-1"></a>Actually I have the same demand, as I need to generate models in python and load my model in c++ and training in c++. I need to set parameter names dynamically in python and get all parameter names in c++, but if I do register, then I got different names point to same parameter.<br>
How ever, I found out that python code will only set parameter name automatically if that parameter is direct class member. so my ugly solution is use a dict to cache parameters:</h2>
<p>class NeuralNetwork(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.param_map = {}<br>
v1 = torch.nn.Parameter(torch.tensor([[[1]]], dtype=torch.float32, requires_grad=True))<br>
v2 = torch.nn.Parameter(torch.tensor([[[1]]], dtype=torch.float32, requires_grad=True))<br>
self.register_parameter(‘weights1’, v1)<br>
self.register_parameter(‘weights2’, v2)<br>
self.param_map[‘v1’] = v1 # mimic self.v1<br>
self.param_map[‘v2’] = v2</p>