From db81ea9298c2dcfecf2ac49aaa708c52933455f3 Mon Sep 17 00:00:00 2001 From: Sharma Date: Tue, 4 Jun 2019 11:13:39 -0700 Subject: [PATCH 1/6] update dumps for const init --- python/mxnet/initializer.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/python/mxnet/initializer.py b/python/mxnet/initializer.py index aca7c58707e2..0a6e6a01672c 100755 --- a/python/mxnet/initializer.py +++ b/python/mxnet/initializer.py @@ -464,6 +464,11 @@ def __init__(self, value): def _init_weight(self, _, arr): arr[:] = self.value + def dumps(self): + if not np.isscalar(self._kwargs['value']): + self._kwargs['value'] = self._kwargs['value'].asnumpy().tolist() + return json.dumps([self.__class__.__name__.lower(), self._kwargs]) + @register class Uniform(Initializer): """Initializes weights with random values uniformly sampled from a given range. From d0db62cb318a73672c785b4c424cfb51df5e42c9 Mon Sep 17 00:00:00 2001 From: Sharma Date: Thu, 6 Jun 2019 12:47:39 -0700 Subject: [PATCH 2/6] add test --- tests/python/unittest/test_init.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/python/unittest/test_init.py b/tests/python/unittest/test_init.py index c8bf01f48ca3..b13a01527caf 100644 --- a/tests/python/unittest/test_init.py +++ b/tests/python/unittest/test_init.py @@ -67,10 +67,19 @@ def test_bilinear_init(): bili_1d = np.array([[1/float(4), 3/float(4), 3/float(4), 1/float(4)]]) bili_2d = bili_1d * np.transpose(bili_1d) assert (bili_2d == bili_weight.asnumpy()).all() - + +def test_const_init_dumps(): + # test NDArray input + init = mx.init.Constant(mx.nd.ones(5)) + assert init.dumps() == '["constant", {"value": [1.0, 1.0, 1.0, 1.0, 1.0]}]' + # test scalar input + init = mx.init.Constant(1) + assert init.dumps() == '["constant", {"value": 1}]' + if __name__ == '__main__': test_variable_init() test_default_init() test_aux_init() test_rsp_const_init() test_bilinear_init() + test_const_init_dumps() From 0452c9cd068d7f20976c8136efdfb91dd3943ed5 Mon Sep 17 00:00:00 2001 From: Sharma Date: Thu, 6 Jun 2019 16:29:55 -0700 Subject: [PATCH 3/6] fix for numpy input --- python/mxnet/initializer.py | 5 +++-- tests/python/unittest/test_init.py | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/python/mxnet/initializer.py b/python/mxnet/initializer.py index 0a6e6a01672c..8b008778c380 100755 --- a/python/mxnet/initializer.py +++ b/python/mxnet/initializer.py @@ -465,8 +465,9 @@ def _init_weight(self, _, arr): arr[:] = self.value def dumps(self): - if not np.isscalar(self._kwargs['value']): - self._kwargs['value'] = self._kwargs['value'].asnumpy().tolist() + val = self._kwargs['value'] + if not np.isscalar(val): + self._kwargs['value'] = val.tolist() if type(val).__module__ == 'numpy' else val.asnumpy().tolist() return json.dumps([self.__class__.__name__.lower(), self._kwargs]) @register diff --git a/tests/python/unittest/test_init.py b/tests/python/unittest/test_init.py index b13a01527caf..bd8a34bc4637 100644 --- a/tests/python/unittest/test_init.py +++ b/tests/python/unittest/test_init.py @@ -75,6 +75,10 @@ def test_const_init_dumps(): # test scalar input init = mx.init.Constant(1) assert init.dumps() == '["constant", {"value": 1}]' + # test numpy input + init = mx.init.Constant(np.ones(5)) + assert init.dumps() == '["constant", {"value": [1.0, 1.0, 1.0, 1.0, 1.0]}]' + if __name__ == '__main__': test_variable_init() From 071a065cc38a6cc2b4f003fece49ae88ae9256c4 Mon Sep 17 00:00:00 2001 From: Sharma Date: Fri, 7 Jun 2019 12:27:07 -0700 Subject: [PATCH 4/6] randomize test array shape and dim --- tests/python/unittest/test_init.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/python/unittest/test_init.py b/tests/python/unittest/test_init.py index bd8a34bc4637..a3bfd89243ba 100644 --- a/tests/python/unittest/test_init.py +++ b/tests/python/unittest/test_init.py @@ -17,6 +17,7 @@ import mxnet as mx import numpy as np +import json def test_default_init(): data = mx.sym.Variable('data') @@ -69,15 +70,18 @@ def test_bilinear_init(): assert (bili_2d == bili_weight.asnumpy()).all() def test_const_init_dumps(): + shape = tuple(np.random.randint(1, 10, size=np.random.randint(1, 5))) # test NDArray input - init = mx.init.Constant(mx.nd.ones(5)) - assert init.dumps() == '["constant", {"value": [1.0, 1.0, 1.0, 1.0, 1.0]}]' + init = mx.init.Constant(mx.nd.ones(shape)) + init._kwargs['value'] = init._kwargs['value'].asnumpy().tolist() + assert init.dumps() == json.dumps([init.__class__.__name__.lower(), init._kwargs]) # test scalar input init = mx.init.Constant(1) assert init.dumps() == '["constant", {"value": 1}]' # test numpy input - init = mx.init.Constant(np.ones(5)) - assert init.dumps() == '["constant", {"value": [1.0, 1.0, 1.0, 1.0, 1.0]}]' + init = mx.init.Constant(np.ones(shape)) + init._kwargs['value'] = init._kwargs['value'].tolist() + assert init.dumps() == json.dumps([init.__class__.__name__.lower(), init._kwargs]) if __name__ == '__main__': From 03b99837c747b84cd2b74c5f30685e8f1be49d54 Mon Sep 17 00:00:00 2001 From: Sharma Date: Fri, 7 Jun 2019 14:25:25 -0700 Subject: [PATCH 5/6] fix test --- tests/python/unittest/test_init.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/python/unittest/test_init.py b/tests/python/unittest/test_init.py index a3bfd89243ba..6d8830c1d089 100644 --- a/tests/python/unittest/test_init.py +++ b/tests/python/unittest/test_init.py @@ -73,15 +73,15 @@ def test_const_init_dumps(): shape = tuple(np.random.randint(1, 10, size=np.random.randint(1, 5))) # test NDArray input init = mx.init.Constant(mx.nd.ones(shape)) - init._kwargs['value'] = init._kwargs['value'].asnumpy().tolist() - assert init.dumps() == json.dumps([init.__class__.__name__.lower(), init._kwargs]) + val = init.dumps() + assert val == json.dumps([init.__class__.__name__.lower(), init._kwargs]) # test scalar input init = mx.init.Constant(1) assert init.dumps() == '["constant", {"value": 1}]' # test numpy input init = mx.init.Constant(np.ones(shape)) - init._kwargs['value'] = init._kwargs['value'].tolist() - assert init.dumps() == json.dumps([init.__class__.__name__.lower(), init._kwargs]) + val = init.dumps() + assert val == json.dumps([init.__class__.__name__.lower(), init._kwargs]) if __name__ == '__main__': From fe8828da9b5766dc0383b5602785ee490d26f49b Mon Sep 17 00:00:00 2001 From: Sharma Date: Tue, 18 Jun 2019 10:15:28 -0700 Subject: [PATCH 6/6] replace type with isinstance --- python/mxnet/initializer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/mxnet/initializer.py b/python/mxnet/initializer.py index 8b008778c380..277bfd0f4fa5 100755 --- a/python/mxnet/initializer.py +++ b/python/mxnet/initializer.py @@ -467,7 +467,7 @@ def _init_weight(self, _, arr): def dumps(self): val = self._kwargs['value'] if not np.isscalar(val): - self._kwargs['value'] = val.tolist() if type(val).__module__ == 'numpy' else val.asnumpy().tolist() + self._kwargs['value'] = val.tolist() if isinstance(val, np.ndarray) else val.asnumpy().tolist() return json.dumps([self.__class__.__name__.lower(), self._kwargs]) @register