My parameters are named like conv1.weight and conv1.bias. VGG’s layers are named like features.0.weight and features.0.bias.
Does anyone have any suggestions?
pretrained_dict = torch.load('VGG_dict.pth')
model_dict = model.state_dict()
# Modification to the dictionary will go here?
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
pre_trained_model=torch.load("Path to the .pth file")
new=list(pre_trained.items())
my_model_kvpair=mymodel.state_dict()
count=0
for key,value in my_model_kvpair.item():
layer_name,weights=new[count]
mymodel_kvpair[key]=weights
count+=1
Thanks a lot. This is exactly what I am looking for.
In addition, do you know a good way to transfer the optimizer state to the new model with different names? The reason I am asking here is that the pre-trained model I have is constructed via nn.Sequential, but I want to insert some new blocks in between and retrain the new network. It will be easier for me if I can start with what they have.
For me on a nested nn, this code piece did not work. Though the dictionary is updated as expected, the weights and bias actually didn’t change. Do you have other recommendations?