记录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) + '"')
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值