参考:
https://blog.csdn.net/LXX516/article/details/80124768

示例代码:

  1. 加载相同名称的模块
pretrained_dict=torch.load(model_weight)
model_dict=myNet.state_dict()
# 1. filter out unnecessary keys
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
# 2. overwrite entries in the existing state dict
model_dict.update(pretrained_dict)
myNet.load_state_dict(model_dict)

  1. 直接赋值
pretrained_dict = torch.load('pre_model/best.pt')
model_dict = self.get_model().state_dict()
model_dict['inBlock.0.0.weight'][:,0:10,:,:] = pretrained_dict['inBlock.0.0.weight'][:,0:10,:,:]
self.get_model().load_state_dict(model_dict)