diff --git a/python/mxnet/gluon/block.py b/python/mxnet/gluon/block.py index 01d32bf0d126..dfcf8ca015ff 100644 --- a/python/mxnet/gluon/block.py +++ b/python/mxnet/gluon/block.py @@ -524,7 +524,9 @@ def load_parameters(self, filename, ctx=None, allow_missing=False, if not any('.' in i for i in loaded.keys()): # legacy loading - del loaded + loaded = None + # We cannot `del loaded` as it is used in a generator expression + # below. `del loaded` would be a SyntaxError in Python 2. self.collect_params().load( filename, ctx, allow_missing, ignore_extra, self.prefix, cast_dtype=cast_dtype, dtype_source=dtype_source) diff --git a/tests/python/unittest/test_gluon.py b/tests/python/unittest/test_gluon.py index e85d8d25bf47..7b389705b9ca 100644 --- a/tests/python/unittest/test_gluon.py +++ b/tests/python/unittest/test_gluon.py @@ -1512,17 +1512,17 @@ def forward(self, x): net2.load_parameters('tmp.params') @with_seed() -def test_save_load_with_shared_params(): +def test_save_load_deduplicate_with_shared_params(): class B(mx.gluon.Block): def __init__(self, params=None): - super().__init__(params=params) + super(B, self).__init__(params=params) with self.name_scope(): self.weight = self.params.get('weight', shape=(10, 10)) class C(mx.gluon.Block): def __init__(self, b1, b2): - super().__init__() + super(C, self).__init__() self.b1 = b1 self.b2 = b2 @@ -1530,7 +1530,7 @@ def __init__(self, b1, b2): b2 = B(b1.collect_params()) c = C(b1, b2) c.initialize() - c.save_parameters('tmp.params') + c.save_parameters('tmp.params', deduplicate=True) params = mx.nd.load('tmp.params') assert len(params) == 1 # Only a single copy of the shared parameter is saved