From 5b3d33bdd1d6a4990c3e6858124993a9b987ab2e Mon Sep 17 00:00:00 2001 From: Jiabin Yang Date: Tue, 27 Aug 2019 21:19:54 +0800 Subject: [PATCH] test=release/1.5, fix problem that get_attr method can't using default mode when we call has_attr in dygraph (#19328) (#19414) * add default getItem * test=develop, fix has_attr disabled error in Layer * test=develop, fix GroupNorm and deepcf bug on attrs --- python/paddle/fluid/dygraph/layers.py | 2 ++ python/paddle/fluid/dygraph/nn.py | 4 ++-- .../paddle/fluid/tests/unittests/test_imperative_basic.py | 7 +++++++ .../paddle/fluid/tests/unittests/test_imperative_deepcf.py | 1 - 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/python/paddle/fluid/dygraph/layers.py b/python/paddle/fluid/dygraph/layers.py index eca8d060b0f..afb18ed505b 100644 --- a/python/paddle/fluid/dygraph/layers.py +++ b/python/paddle/fluid/dygraph/layers.py @@ -212,6 +212,8 @@ class Layer(core.Layer): return self._parameters[name] elif name in self._sub_layers: return self._sub_layers[name] + else: + return object.__getattribute__(self, name) def __setattr__(self, name, value): if isinstance(value, framework.Parameter): diff --git a/python/paddle/fluid/dygraph/nn.py b/python/paddle/fluid/dygraph/nn.py index f933e22ddfa..8e78d8d0089 100644 --- a/python/paddle/fluid/dygraph/nn.py +++ b/python/paddle/fluid/dygraph/nn.py @@ -2403,9 +2403,9 @@ class GroupNorm(layers.Layer): def forward(self, input): inputs = {'X': input} - if self._bias: + if self._bias_attr: inputs['Bias'] = self._bias - if self._scale: + if self._param_attr: inputs['Scale'] = self._scale # create output diff --git a/python/paddle/fluid/tests/unittests/test_imperative_basic.py b/python/paddle/fluid/tests/unittests/test_imperative_basic.py index afa21a375a4..dd721f6671f 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_basic.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_basic.py @@ -405,6 +405,13 @@ class TestImperative(unittest.TestCase): self.assertTrue(np.allclose(dy_grad_h2h2, static_grad_h2h)) self.assertTrue(np.allclose(dy_grad_i2h2, static_grad_i2h)) + def test_layer_attrs(self): + layer = fluid.dygraph.Layer("test") + layer.test_attr = 1 + self.assertFalse(hasattr(layer, "whatever")) + self.assertTrue(hasattr(layer, "test_attr")) + self.assertEqual(layer.test_attr, 1) + if __name__ == '__main__': unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_imperative_deepcf.py b/python/paddle/fluid/tests/unittests/test_imperative_deepcf.py index daf8cc00d43..579b073d082 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_deepcf.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_deepcf.py @@ -73,7 +73,6 @@ class MLP(fluid.Layer): self.add_sublayer( 'match_layer_%d' % i, fluid.FC(self.full_name(), self._hid_sizes[i], act='relu'))) - self._mat def forward(self, users, items): users = self._user_latent(users) -- GitLab