You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by zh...@apache.org on 2018/03/28 08:56:50 UTC

[incubator-mxnet] branch master updated: support loading parameter in partially declared gluon params (#10287)

This is an automated email from the ASF dual-hosted git repository.

zhasheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/master by this push:
     new 6988316  support loading parameter in partially declared gluon params (#10287)
6988316 is described below

commit 69883163ef8e76c22a1d2cd7deb219f04a9ce82f
Author: Sheng Zha <sz...@users.noreply.github.com>
AuthorDate: Wed Mar 28 01:56:45 2018 -0700

    support loading parameter in partially declared gluon params (#10287)
    
    * support loading parameter in partially declared gluon params
    
    * add partial shape match in parameter.get
    
    * fix lint
---
 python/mxnet/gluon/parameter.py     | 23 +++++++++++++++++++++--
 tests/python/unittest/test_gluon.py | 18 ++++++++++++++----
 2 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/python/mxnet/gluon/parameter.py b/python/mxnet/gluon/parameter.py
index 7dc7243..5a9277b 100644
--- a/python/mxnet/gluon/parameter.py
+++ b/python/mxnet/gluon/parameter.py
@@ -574,13 +574,32 @@ class ParameterDict(object):
         """
         name = self.prefix + name
         param = self._get_impl(name)
-        if param is None:
+        if param is None: # pylint: disable=too-many-nested-blocks
             param = Parameter(name, **kwargs)
             self._params[name] = param
         else:
             for k, v in kwargs.items():
                 if hasattr(param, k) and getattr(param, k) is not None:
-                    assert v is None or v == getattr(param, k), \
+                    existing = getattr(param, k)
+                    if k == 'shape' and len(v) == len(existing):
+                        inferred_shape = []
+                        matched = True
+                        for dim1, dim2 in zip(v, existing):
+                            if dim1 != dim2 and dim1 * dim2 != 0:
+                                matched = False
+                                break
+                            elif dim1 == dim2:
+                                inferred_shape.append(dim1)
+                            elif dim1 == 0:
+                                inferred_shape.append(dim2)
+                            else:
+                                inferred_shape.append(dim1)
+
+                        if matched:
+                            param._shape = tuple(inferred_shape)
+                            continue
+
+                    assert v is None or v == existing, \
                         "Cannot retrieve Parameter %s because desired attribute " \
                         "does not match with stored for attribute %s: " \
                         "desired %s vs stored %s."%(
diff --git a/tests/python/unittest/test_gluon.py b/tests/python/unittest/test_gluon.py
index ba2e7ab..952fdf7 100644
--- a/tests/python/unittest/test_gluon.py
+++ b/tests/python/unittest/test_gluon.py
@@ -82,16 +82,16 @@ def test_constant():
 @with_seed()
 def test_parameter_sharing():
     class Net(gluon.Block):
-        def __init__(self, **kwargs):
+        def __init__(self, in_units=0, **kwargs):
             super(Net, self).__init__(**kwargs)
             with self.name_scope():
-                self.dense0 = nn.Dense(5, in_units=5)
-                self.dense1 = nn.Dense(5, in_units=5)
+                self.dense0 = nn.Dense(5, in_units=in_units)
+                self.dense1 = nn.Dense(5, in_units=in_units)
 
         def forward(self, x):
             return self.dense1(self.dense0(x))
 
-    net1 = Net(prefix='net1_')
+    net1 = Net(prefix='net1_', in_units=5)
     net2 = Net(prefix='net2_', params=net1.collect_params())
     net1.collect_params().initialize()
     net2(mx.nd.zeros((3, 5)))
@@ -101,6 +101,16 @@ def test_parameter_sharing():
     net3 = Net(prefix='net3_')
     net3.load_params('net1.params', mx.cpu())
 
+    net4 = Net(prefix='net4_')
+    net5 = Net(prefix='net5_', in_units=5, params=net4.collect_params())
+    net4.collect_params().initialize()
+    net5(mx.nd.zeros((3, 5)))
+
+    net4.save_params('net4.params')
+
+    net6 = Net(prefix='net6_')
+    net6.load_params('net4.params', mx.cpu())
+
 
 @with_seed()
 def test_parameter_str():

-- 
To stop receiving notification emails like this one, please contact
zhasheng@apache.org.