记录Python中Tensor size mismatch的解决方案

深度学习模型训练完毕之后,在加载模型进行Test验证时,出现以下报错:

RuntimeError: Error(s) in loading state_dict for GWNet:
	Unexpected key(s) in state_dict: "nodevec1", "nodevec2", "cat_feature_conv.weight", "cat_feature_conv.bias". 
	size mismatch for start_conv.weight: copying a param with shape torch.Size([40, 1, 1, 1]) from checkpoint, the shape in current model is torch.Size([40, 2, 1, 1]).
	size mismatch for graph_convs.0.final_conv.weight: copying a param with shape torch.Size([40, 280, 1, 1]) from checkpoint, the shape in current model is torch.Size([40, 200, 1, 1]).
	size mismatch for graph_convs.1.final_conv.weight: copying a param with shape torch.Size([40, 280, 1, 1]) from checkpoint, the shape in current model is torch.Size([40, 200, 1, 1]).
	size mismatch for graph_convs.2.final_conv.weight: copying a param with shape torch.Size([40, 280, 1, 1]) from checkpoint, the shape in current model is torch.Size([40, 200, 1, 1]).
	size mismatch for graph_convs.3.final_conv.weight: copying a param with shape torch.Size([40, 280, 1, 1]) from checkpoint, the shape in current model is torch.Size([40, 200, 1, 1]).
	size mismatch for graph_convs.4.final_conv.weight: copying a param with shape torch.Size([40, 280, 1, 1]) from checkpoint, the shape in current model is torch.Size([40, 200, 1, 1]).
	size mismatch for graph_convs.5.final_conv.weight: copying a param with shape torch.Size([40, 280, 1, 1]) from checkpoint, the shape in current model is torch.Size([40, 200, 1, 1]).
	size mismatch for graph_convs.6.final_conv.weight: copying a param with shape torch.Size([40, 280, 1, 1]) from checkpoint, the shape in current model is torch.Size([40, 200, 1, 1]).
	size mismatch for graph_convs.7.final_conv.weight: copying a param with shape torch.Size([40, 280, 1, 1]) from checkpoint, the shape in current model is torch.Size([40, 200, 1, 1]).

分析原因是前后的tensor出现了不匹配的错误,但是python会默认打印报错。
因此,其中的一种解决方案是让其直接跳过报错部分,解决方案如下。
将原代码中的加载模型语句:

model.load_state_dict(torch.load(args.checkpoint))

替换为以下即可:

try:
    model.load_state_dict(torch.load(args.checkpoint))
except RuntimeError as e:
    print('Ignoring "' + str(e) + '"')

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