Rename nn.Parameter

Hi all,

I have been trying to obtain the following behavior when defining an own model: In essence, what I would want is to have a Parameter whose name would be weights, so that in model.state_dict() I would see weights. 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:

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()

With this code, I can call model.W, but also model.weights. Plus, when I call model.state_dict(), then I obtain a list of two parameters, “W” and “weights”.

I wanted to ask, firstly, if the kind of behavior I would desire (being able to call only to model.W and having only “weights” in model.state_dict(), both referring to the same Parameter) is common/good practice, and if so, how to implement it properly.

Thanks in advance!

No! This is decidedly not good style. You are trading conceptual simplicity for saving a few characters (6 instead of 12).

Now, what you can do if your forward uses self.weights so often that you don’t want to spell it?
I think a perfectly good solution is to do a local assignment W = self.weights at the beginning of the forward (or whatever method uses W). If you use it often enough, it’ll even be less characters.

Best regards


1 Like

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 W 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 Wto weightseverywhere. Thank you very much for the clarification.