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!