piiswrong closed pull request #8956: Fix __repr__ for gluon.Parameter URL: https://github.com/apache/incubator-mxnet/pull/8956
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 1c42e03958..9d8542ec2d 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -152,3 +152,4 @@ List of Contributors * [Andre Tamm](https://github.com/andretamm) * [Marco de Abreu](https://github.com/marcoabreu) - Marco is the creator of the current MXNet CI. +* [Julian Salazar](https://github.com/JulianSlzr) diff --git a/python/mxnet/gluon/parameter.py b/python/mxnet/gluon/parameter.py index 537d6365a4..fa38285911 100644 --- a/python/mxnet/gluon/parameter.py +++ b/python/mxnet/gluon/parameter.py @@ -117,7 +117,7 @@ def __init__(self, name, grad_req='write', shape=None, dtype=mx_real_t, def __repr__(self): s = 'Parameter {name} (shape={shape}, dtype={dtype})' - return s.format(**self.__dict__) + return s.format(name=self.name, shape=self.shape, dtype=self.dtype) @property def grad_req(self): diff --git a/tests/python/unittest/test_gluon.py b/tests/python/unittest/test_gluon.py index c619056c11..f2d001a702 100644 --- a/tests/python/unittest/test_gluon.py +++ b/tests/python/unittest/test_gluon.py @@ -70,6 +70,23 @@ def forward(self, x): net3.load_params('net1.params', mx.cpu()) +def test_parameter_str(): + class Net(gluon.Block): + def __init__(self, **kwargs): + super(Net, self).__init__(**kwargs) + with self.name_scope(): + self.dense0 = nn.Dense(10, in_units=5, use_bias=False) + + net = Net(prefix='net1_') + lines = str(net.collect_params()).splitlines() + + assert lines[0] == 'net1_ (' + assert 'net1_dense0_weight' in lines[1] + assert '(10, 5)' in lines[1] + assert 'numpy.float32' in lines[1] + assert lines[2] == ')' + + def test_basic(): model = nn.Sequential() model.add(nn.Dense(128, activation='tanh', in_units=10, flatten=False)) ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services