In PyTorch, one can define parameters in the forward method rather than in the init method (when their shape depends on the size of the inputs).
Small example (as explained in this thread):
class MyModule(nn.Module):
def __init__(self):
# you need to register the parameter names earlier
self.register_parameter('weight', None)
def forward(self, input):
if self.weight is None:
self.weight = nn.Parameter(torch.randn(input.size()))
return self.weight @ input
However, how can I load the parameters of a given state dict into such a model? (i.e. suppose I have a state_dict with the âweightâ parameter). Currently Iâm getting this:
RuntimeError: Error(s) in loading state_dict for MyModule:
Unexpected key(s) in state_dict: "weight".
Second question: the code above will not work when moving the model to GPU, as model.cuda() is usually called before forward(). How can I make it work both on CPU and GPU?