# Error loading saved Pytorch neural network

**URL:** <https://discuss.pytorch.org/t/error-loading-saved-pytorch-neural-network/116908>\
**Category:** Uncategorized\
**Created:** [April 3, 2021, 3:09am UTC](https://discuss.pytorch.org/t/error-loading-saved-pytorch-neural-network/116908 "2021-04-03T03:09:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![s\_mkb](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@s\_mkb](https://discuss.pytorch.org/u/s_mkb)\
**Post date:** [April 3, 2021, 3:09am UTC](https://discuss.pytorch.org/t/error-loading-saved-pytorch-neural-network/116908/1 "2021-04-03T03:09:35Z")

</div>

Hello. I am having trouble loading a saved Pytorch network and I have no idea why. I have done this several times before, and I have never had this error. This is the error I get:

```auto
RuntimeError: Error(s) in loading state_dict for NeuralNet:
	Unexpected key(s) in state_dict: "conv1.weight", "conv1.bias", "conv2.weight", "conv2.bias".

```

This is how I load my model:

```auto
model = NeuralNet()
model.load_state_dict(torch.load('./results/model_mar30_batch10.pth'))
model.eval()

```

This is my neural network structure:

```auto
    def __init__ (self, **kwargs):
        super(NeuralNet, self). __init__ (**kwargs)
        
        self.conv3 = nn.Conv2d(in_channels=24, out_channels=256, kernel_size=(2,2), stride=(2,2)) 
        self.conv4 = nn.Conv2d(in_channels=256, out_channels=512, kernel_size=(10,6), stride=(1,1))
    
        self.batch3 = nn.BatchNorm2d(512)
        self.batch4 = nn.BatchNorm2d(256)
        self.batch = nn.BatchNorm2d(24)

        #DECONVOLUTION

        self.deconv1 = nn.ConvTranspose2d(in_channels=512, out_channels=256, kernel_size=(10,6), stride=(1,1))
        self.deconv2 = nn.ConvTranspose2d(in_channels=256, out_channels=24, kernel_size=(2,2), stride=(2,2), padding=0)
        self.deconv3 = nn.ConvTranspose2d(in_channels=24, out_channels=3, kernel_size=(5,5), stride=(5,5), padding=0)

    def SqueezeExcite(self,channels,reduction,x):
        
        mid_cannels = channels // reduction

        self.pool = nn.AdaptiveAvgPool2d(output_size=1)
        self.conv1 = nn.Conv2d(in_channels=channels, out_channels=mid_cannels, kernel_size=(1,1), stride=(1,1), groups=1, bias=True)
        self.activ = nn.ReLU()
        self.conv2 = nn.Conv2d(in_channels=mid_cannels, out_channels=channels, kernel_size=(1,1), stride=(1,1), groups=1, bias=True)
        self.sigmoid = nn.Sigmoid()
        w = self.pool(x)
        w = self.conv1(w.float())
        w = self.activ(w.float())
        w = self.conv2(w.float())
        w = self.sigmoid(w.float())
        x = x * w.float()
        return x.float()
        
        encode1 = self.batch(torch.tanh(func.conv2d(input=x_in, weight=weights, stride=(5,5), padding=(0,0), groups=3)))

        encode2 = self.batch4(torch.tanh(self.conv3(encode1)))
        encode2 = self.SqueezeExcite(channels=256, reduction=16, x=encode2.float())
        encode3 = self.batch3(torch.tanh(self.conv4(encode2)))
  
        decode1 = torch.tanh(self.deconv1(encode3))
        decode5 = torch.tanh(self.deconv2(decode1))
        decode6 = func.relu(self.deconv3(decode5))
  
        decode6[:,0,:,:] = decode6[:,0,:,:] * x[:,0,:,:]
        decode6[:,1,:,:] = decode6[:,1,:,:] * x[:,0,:,:]
        decode6[:,2,:,:] = decode6[:,2,:,:] * x[:,0,:,:]
        
        return decode6

```

Usually I do not have problems, but recently I added this SqueezeAndExcitation block, and the error has occurred.

---

<div class="post-metadata">

**Author:** ![Monne](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@Monne](https://discuss.pytorch.org/u/Monne)\
**Post date:** [April 3, 2021, 3:10am UTC](https://discuss.pytorch.org/t/error-loading-saved-pytorch-neural-network/116908/2 "2021-04-03T03:10:51Z")

</div>

if im not mistaken, you try to load conv1 weight but in ` __init__ ` it got commented. also im not seeing conv2 either in ` __init__ `

---

<div class="post-metadata">

**Author:** ![s\_mkb](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@s\_mkb](https://discuss.pytorch.org/u/s_mkb)\
**Post date:** [April 3, 2021, 3:13am UTC](https://discuss.pytorch.org/t/error-loading-saved-pytorch-neural-network/116908/3 "2021-04-03T03:13:33Z")

</div>

Sorry, I don’t use that conv layer anymore. Just removed the comment to avoid confusion.

If that was the issue my training wouldn’t have run. It ran and I saved the trained model, I just cannot load it for some reason.

---

<div class="post-metadata">

**Author:** ![Monne](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@Monne](https://discuss.pytorch.org/u/Monne)\
**Post date:** [April 3, 2021, 3:15am UTC](https://discuss.pytorch.org/t/error-loading-saved-pytorch-neural-network/116908/4 "2021-04-03T03:15:48Z")

</div>

but the saved model you load had conv1 and conv2 weight, maybe you load weight before edit the model?

---

<div class="post-metadata">

**Author:** ![s\_mkb](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@s\_mkb](https://discuss.pytorch.org/u/s_mkb)\
**Post date:** [April 3, 2021, 3:18am UTC](https://discuss.pytorch.org/t/error-loading-saved-pytorch-neural-network/116908/5 "2021-04-03T03:18:03Z")

</div>

Yes, those are coming from the SqueezeExcite function.

---

<div class="post-metadata">

**Author:** ![Monne](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@Monne](https://discuss.pytorch.org/u/Monne)\
**Post date:** [April 3, 2021, 3:28am UTC](https://discuss.pytorch.org/t/error-loading-saved-pytorch-neural-network/116908/6 "2021-04-03T03:28:42Z")

</div>

maybe it cannot detect latter parameter assignment, since there is no conv1 and conv2 in the first place. can you check what model.parameters() or model.state\_dict().keys() before load model?

---

<div class="post-metadata">

**Author:** ![s\_mkb](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/b5a626/32.png) [@s\_mkb](https://discuss.pytorch.org/u/s_mkb)\
**Post date:** [April 3, 2021, 7:09pm UTC](https://discuss.pytorch.org/t/error-loading-saved-pytorch-neural-network/116908/7 "2021-04-03T19:09:30Z")

</div>

That is true. It worked once I removed the function, and just assigned conv1 and conv2 without them being inside a function. Thanks!
