pytorch 添新层方法,报错Input type (torch.cuda.FloatTensor) ,weight type (torch.FloatTensor) should same

error,Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

###在 改动模型的时候,添加已有模型新层,添加方式,如下正确
在初始化添加self.add=nn.Conv2d(1408, 2048, kernel_size=1)

def forward(self, inputs):
        """ Calls extract_features to extract features, applies final linear layer, and returns logits. """

        # Convolution layers
        x = self.extract_features(inputs)
        x= self.add(x)
        return x

不能下面直接添加,否则报错,添加的网络实际也没有添加到网络中
print model 没有

def forward(self, inputs):
        """ Calls extract_features to extract features, applies final linear layer, and returns logits. """

        # Convolution layers
        x = self.extract_features(inputs)
        x= self.add(x)
        ######
        add = nn.Conv2d(1408, 2048, kernel_size=1)
        x=add(x)
        ##########
        return x

2、添加新层,以及添加新层加载旧模型的参数,方法 strict

pretrained_net = torch.load('t.pth')
new_network = Net_new()
new_network.load_state_dict(pretrained_net, strict=False)
for key, _ in new_network.state_dict().items():
    print key

详细内容
https://blog.csdn.net/hungryof/article/details/81364487

3、加载模型的部分参数

详细内容
https://blog.csdn.net/m0_37192554/article/details/103079896

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值