size mismatch for xx.weight错误的解决方法

问题重现:

RuntimeError: Error(s) in loading state_dict for xxxNet:
	size mismatch for bn1.weight: copying a param with shape torch.Size([3]) from checkpoint, the shape in current model is torch.Size([512]).
	size mismatch for bn1.bias: copying a param with shape torch.Size([3]) from checkpoint, the shape in current model is torch.Size([512]).
	size mismatch for bn1.running_mean: copying a param with shape torch.Size([3]) from checkpoint, the shape in current model is torch.Size([512]).
	size mismatch for bn1.running_var: copying a param with shape torch.Size([3]) from checkpoint, the shape in current model is torch.Size([512]).

问题分析:

在使用load_state_dict(model_dict, False)时会报错,原因是你现有的模型和你权重文件的保存的模型结构不一样,但是我现有的模型结构和原来是同一个,为什么还会报错呢?
原因是:你现有的模型内有一些已定义,但是没有使用的层,比如在我上面的报错中,bn1层在我们模型的__init__里面定义了,但是没有在forward中使用,但是load_state_dict()仍然会把权重文件.pth里面的bn层赋值给bn1层,导致层的对应关系错误,所以要把bn1去掉就可以了。

size mismatch for xx.weight错误的解决方法_第1张图片
size mismatch for xx.weight错误的解决方法_第2张图片

你可能感兴趣的:(debug,pytorch,深度学习)