ScriptModule `register_parameter`

I wonder if register_parameter is expected to work with ScriptdModules, for example:

import torch
module = torch.jit.trace(torch.nn.Linear(10, 10), torch.randn(100, 10))
module.register_parameter("new_parameter", torch.nn.Parameter(torch.randn(10, 10)))

AttributeError: cannot assign parameter before Module.init() call

I don’t have an specific use case for this… I am implementing a ScriptModule wrapper for R’s implementation of torch and wanted to know if this is expected to wok.

I don’t think this would be a supported use case, since the already traced model won’t have a chance to use the newly registered parameter afterwards.
In the case that this parameter is indeed used in the forward (but not registered yet), tracing would raise an error due to the usage of undefined parameters. On the other hand, if this parameter is never used in the forward, registering it afterwards won’t change anything, so I assume the error is expected.

@ptrblck Thanks for your explanation!

In theory one could use register_parameter to overwrite weights, eg:

import torch
module = torch.jit.trace(torch.nn.Linear(10, 10), torch.randn(100, 10))
module.register_parameter("weight", torch.nn.Parameter(torch.ones(10, 10)))

And expect it to be used by the ScriptModule. This seems to work fine in non-scripted modules.
But there are other ways to do it anyway.