使用以下方法保存Py手火炬模型(图、权重和偏倚):
torch.save(self.state_dict(), file)满载:
self.load_state_dict(torch.load(file))但是,如果参数被更改,模型将不会加载错误--例如:
RuntimeError: Error(s) in loading state_dict for LeNet5:
size mismatch for conv1.weight:是否可以在改变大小的情况下加载到模型中?在初始化(如果有更多的权重)和剪辑(如果有较少的权重)的剩余权重?
发布于 2019-06-30 13:14:33
没有自动的方法来做到这一点,因为当事情不匹配时,您需要显式地来决定该做什么。
就我个人而言,当我需要“强制”一个预先训练的重量在一个稍微改变的模型。我发现使用state_dict本身是最方便的方法。
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)https://stackoverflow.com/questions/56825055
复制相似问题