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

发布了98 篇原创文章 · 获赞 141 · 访问量 26万+

猜你喜欢

转载自blog.csdn.net/m0_37192554/article/details/103071862