copying a param of torch.Size([128]) from checkpoint, where the shape is torch.Size([1, 128])解决方式

6 篇文章 0 订阅
5 篇文章 0 订阅

关于torch的在加载bn_inception、x_inception时出现copying a param of torch.Size([128]) from checkpoint, where the shape is torch.Size([1, 128])类型的错误,观察发现可以用过修改加载模型中权重维度解决问题。

例如:bn_inception中都是两维元组的数据转为一维:如(1,128)转为(128)可使用一下程序解决,类似问题可通过微调以下代码解决:

import torch
state_dict = torch.load('~.torch/models/bn_inception-9f5701afb96c8044.pth')
for name, weights in state_dict.items():
    # print(name, weights.size())  可以查看模型中的模型名字和权重维度
    if len(weights.size()) == 2: 判断需要修改维度的条件
        state_dict[name] = weights.squeeze(0)  去掉维度0,把(1,128)转为(128)
       # print(name,weights.squeeze(0).size()) 查看转化后的模型名字和权重维度
torch.save(state_dict, '~.torch/models/bn_inception-9f5701afb96c8044.pth')

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值