user8426627
user8426627

Reputation: 943

Pytorch: Is it possible to load model when parameters or size have changed?

A Pytorch model (graph, weights, and biases) is saved with :

torch.save(self.state_dict(), file)

and loaded with :

self.load_state_dict(torch.load(file))

But if parameters are changed, the model won't load with error for example :

RuntimeError: Error(s) in loading state_dict for LeNet5:
    size mismatch for conv1.weight:

Is it possible to load to the model with changed size? Sort of filling the rest of weights like in initialization (if there are more weights) and clip if there are fewer weights?

Upvotes: 1

Views: 2952

Answers (1)

Shai
Shai

Reputation: 114976

There is no automatic way of doing so - because you need to explicitly decide what to do when things do not match.

Personally, when I need to "force" a pre-trained weights on a slightly changed model. I find that working with the state_dict itself is the most convenient way.

new_model = model( ... )  # construct the new model
new_sd = new_model.state_dict()  # take the "default" state_dict
pre_trained_sd = torch.load(file)  # load the old version pre-trained weights
# merge information from pre_trained_sd into new_sd
# ...
# after merging the state dict you can load it:
new_model.load_state_dict(new_sd)

Upvotes: 1

Related Questions