From 54f4d58553afc2f326a4d9dda168a8c4a13ccb8e Mon Sep 17 00:00:00 2001 From: Xin Pan Date: Fri, 15 Feb 2019 19:13:47 +0800 Subject: [PATCH] make parameter and layer access easier test=develop --- python/paddle/fluid/imperative/layers.py | 51 +++++++++++++++++++ python/paddle/fluid/imperative/nn.py | 3 -- .../fluid/tests/unittests/test_imperative.py | 12 +++++ .../unittests/test_imperative_ptb_rnn.py | 16 ------ .../tests/unittests/test_imperative_resnet.py | 26 +++++----- 5 files changed, 75 insertions(+), 33 deletions(-) diff --git a/python/paddle/fluid/imperative/layers.py b/python/paddle/fluid/imperative/layers.py index da8233fe394..59fe6bbf74b 100644 --- a/python/paddle/fluid/imperative/layers.py +++ b/python/paddle/fluid/imperative/layers.py @@ -36,6 +36,12 @@ class Layer(core.Layer): def parameters(self, include_sublayers=True): """Returns a list of Parameters from current and sub-layers. + + Args: + include_sublayers: If true, also include the parameters from + sublayers. + + Returns a list of Parameters. """ ret = [p for p in self._parameters.values()] if include_sublayers: @@ -44,6 +50,21 @@ class Layer(core.Layer): ret.append(p) return ret + def sublayers(self, include_sublayers=True): + """Returns a list of sub layers. + + Args: + include_sublayers: If true, also include the layers from sublayers. + + Returns a list of sub layers. + """ + ret = [l for l in self._sub_layers.values()] + if include_sublayers: + for l in self._sub_layers.values(): + for sub_l in l.sublayers(include_sublayers): + ret.append(sub_l) + return ret + def clear_gradients(self): for p in self.parameters(): p._clear_gradient() @@ -65,6 +86,36 @@ class Layer(core.Layer): def backward(self, *inputs): raise ValueError("Layer shouldn't implement backward") + def add_sublayer(self, name, sublayer): + """Adds a sub Layer instance. + + Added sublayer can be access like self.name. + + Args: + name: name of this sublayer. + sublayer: an instance of Layer. + Returns: + the sublayer passed in. + """ + assert isinstance(sublayer, core.Layer) + self._sub_layers[name] = sublayer + return sublayer + + def add_parameter(self, name, parameter): + """Adds a Parameter instance. + + Added parameter can be access like self.name. + + Args: + name: name of this sublayer. + parameter: an instance of Parameter. + Returns: + the parameter passed in. + """ + assert isinstance(parameter, framework.Parameter) + self._parameters[name] = parameter + return parameter + def __getattr__(self, name): if name in self._parameters: return self._parameters[name] diff --git a/python/paddle/fluid/imperative/nn.py b/python/paddle/fluid/imperative/nn.py index 1b0a60df8bc..c86a373ae4a 100644 --- a/python/paddle/fluid/imperative/nn.py +++ b/python/paddle/fluid/imperative/nn.py @@ -475,9 +475,6 @@ class Embedding(layers.Layer): dtype=self._dtype, is_bias=False) - def parameters(self): - return [self._w] - def forward(self, input): out = self._helper.create_variable_for_type_inference(self._dtype) self._helper.append_op( diff --git a/python/paddle/fluid/tests/unittests/test_imperative.py b/python/paddle/fluid/tests/unittests/test_imperative.py index baaddf9f2e5..c54e998ea87 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative.py +++ b/python/paddle/fluid/tests/unittests/test_imperative.py @@ -333,6 +333,18 @@ class TestImperative(unittest.TestCase): self.assertTrue(np.allclose(dy_out, static_out)) self.assertTrue(np.allclose(dy_grad, static_grad)) + params = mlp.parameters(True) + self.assertEqual("FC_0.w_0", params[0].name) + self.assertEqual("FC_0.b_0", params[1].name) + self.assertEqual("FC_1.w_0", params[2].name) + self.assertEqual("FC_1.b_0", params[3].name) + self.assertEqual(len(params), 4) + + sublayers = mlp.sublayers(True) + self.assertEqual(mlp._fc1, sublayers[0]) + self.assertEqual(mlp._fc2, sublayers[1]) + self.assertEqual(len(sublayers), 2) + def test_rnn(self): np_inp = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [10.0, 11.0, 12.0]]) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py b/python/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py index afe990e74ff..82aff18b728 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py @@ -75,16 +75,6 @@ class SimpleLSTMRNN(fluid.imperative.Layer): self.hidden_array.append(pre_hidden) self.cell_array.append(pre_cell) - def parameters(self): - parameters = list() - for param in self.weight_1_arr: - parameters.append(param) - for param in self.weight_2_arr: - parameters.append(param) - for bias in self.bias_arr: - parameters.append(bias) - return parameters - def forward(self, input_embedding, init_hidden=None, init_cell=None): res = [] for index in range(self._num_steps): @@ -177,12 +167,6 @@ class PtbModel(fluid.imperative.Layer): def _build_once(self, input, label, init_hidden, init_cell): pass - def parameters(self): - parameters = self.simple_lstm_rnn.parameters() + [ - self.softmax_weight, self.softmax_bias - ] + self.embedding.parameters() - return parameters - def forward(self, input, label, init_hidden, init_cell): init_h = fluid.layers.reshape( diff --git a/python/paddle/fluid/tests/unittests/test_imperative_resnet.py b/python/paddle/fluid/tests/unittests/test_imperative_resnet.py index c27fd0b8024..128d18621db 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_resnet.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_resnet.py @@ -21,7 +21,6 @@ import paddle import paddle.fluid as fluid from paddle.fluid import core from paddle.fluid.layer_helper import LayerHelper -from paddle.fluid.optimizer import SGDOptimizer from paddle.fluid.imperative.nn import Conv2D, Pool2D, BatchNorm, FC from paddle.fluid.imperative.base import to_variable from test_imperative_base import new_program_scope @@ -173,11 +172,13 @@ class ResNet(fluid.imperative.Layer): for block in range(len(depth)): shortcut = False for i in range(depth[block]): - bottleneck_block = BottleneckBlock( - num_channels=num_channels, - num_filters=num_filters[block], - stride=2 if i == 0 and block != 0 else 1, - shortcut=shortcut) + bottleneck_block = self.add_sublayer( + 'bb_%d_%d' % (block, i), + BottleneckBlock( + num_channels=num_channels, + num_filters=num_filters[block], + stride=2 if i == 0 and block != 0 else 1, + shortcut=shortcut)) num_channels = bottleneck_block._num_channels_out self.bottleneck_block_list.append(bottleneck_block) shortcut = True @@ -223,8 +224,7 @@ class TestImperativeResnet(unittest.TestCase): batch_size=batch_size) dy_param_init_value = {} - for param in fluid.default_main_program().global_block( - ).all_parameters(): + for param in resnet.parameters(): dy_param_init_value[param.name] = param._numpy() for batch_id, data in enumerate(train_reader()): @@ -247,16 +247,14 @@ class TestImperativeResnet(unittest.TestCase): dy_out = avg_loss._numpy() if batch_id == 0: - for param in fluid.default_main_program().global_block( - ).all_parameters(): + for param in resnet.parameters(): if param.name not in dy_param_init_value: dy_param_init_value[param.name] = param._numpy() avg_loss._backward() dy_grad_value = {} - for param in fluid.default_main_program().global_block( - ).all_parameters(): + for param in resnet.parameters(): if not param.stop_gradient: np_array = np.array(param._ivar._grad_ivar().value() .get_tensor()) @@ -267,8 +265,7 @@ class TestImperativeResnet(unittest.TestCase): resnet.clear_gradients() dy_param_value = {} - for param in fluid.default_main_program().global_block( - ).all_parameters(): + for param in resnet.parameters(): dy_param_value[param.name] = param._numpy() with new_program_scope(): @@ -349,6 +346,7 @@ class TestImperativeResnet(unittest.TestCase): self.assertTrue(np.allclose(static_out, dy_out)) self.assertEqual(len(dy_param_init_value), len(static_param_init_value)) + for key, value in six.iteritems(static_param_init_value): self.assertTrue(np.allclose(value, dy_param_init_value[key])) self.assertTrue(np.isfinite(value.all())) -- GitLab