You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2019/09/26 01:11:19 UTC

[GitHub] [incubator-mxnet] WenmuZhou commented on issue #16232: how can I load a pre-trained model with different shape and same name of last layer

WenmuZhou commented on issue #16232: how can I load a pre-trained model with different shape and same name of last layer
URL: https://github.com/apache/incubator-mxnet/issues/16232#issuecomment-535285619
 
 
   a simple code to reproduce is
   ```python
   # -*- coding: utf-8 -*-
   # @Time    : 2019/9/26 8:56
   # @Author  : zhoujun
   from mxnet import gluon
   from mxnet import nd
   
   class Net(gluon.nn.HybridBlock):
       def __init__(self,out):
           super().__init__()
           self.conv = gluon.nn.Conv2D(16,kernel_size=3)
           self.fc = gluon.nn.Dense(out)
       def hybrid_forward(self, F, x, *args, **kwargs):
           x = self.conv(x)
           return self.fc(x)
   
   
   if __name__ == '__main__':
       net = Net(10)
       net.initialize()
       input = nd.random.randn(1,3,20,20)
       y = net(input)
       print(y.shape)
       net.save_parameters('net.params')
   
       net1 = Net(11)
       net1.load_parameters('net.params')
   ```
   
   and the output is 
   ```bash
   (1, 10)
   Traceback (most recent call last):
     File "E:/zj/code/crnn.pytorch/mxnet_t.py", line 26, in <module>
       net1.load_parameters('net.params')
     File "D:\Anaconda3\lib\site-packages\mxnet\gluon\block.py", line 410, in load_parameters
       params[name]._load_init(loaded[name], ctx, cast_dtype=cast_dtype, dtype_source=dtype_source)
     File "D:\Anaconda3\lib\site-packages\mxnet\gluon\parameter.py", line 279, in _load_init
       self.name, str(self.shape), str(data.shape))
   AssertionError: Failed loading Parameter 'dense1_weight' from saved params: shape incompatible expected (11, 0) vs saved (10, 5184)
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services