From bfa2621fc3d63d8b6e0dca79c740e6ccc406a24c Mon Sep 17 00:00:00 2001 From: Xin Pan Date: Wed, 16 Jan 2019 10:13:04 +0800 Subject: [PATCH] fix bias test=develop --- python/paddle/fluid/imperative/nn.py | 14 ++++------ python/paddle/fluid/layer_helper.py | 3 +- .../tests/unittests/test_imperative_gan.py | 28 +++++++++++-------- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/python/paddle/fluid/imperative/nn.py b/python/paddle/fluid/imperative/nn.py index 337a463041b..95b59487661 100644 --- a/python/paddle/fluid/imperative/nn.py +++ b/python/paddle/fluid/imperative/nn.py @@ -221,13 +221,10 @@ class FC(layers.Layer): from ..layer_helper import LayerHelper self._helper = LayerHelper( 'FC', param_attr=param_attr, act=act, name=name) - self._bias_attr = bias_attr + self._bias_attr = bias_attr if bias_attr else ParamAttr() def parameters(self): - if self._bias_attr: - return [self._w, self._b] - else: - return [self._w] + return [self._w, self._b] def _build_once(self, input): input_shape = input.shape @@ -264,10 +261,11 @@ class FC(layers.Layer): # add bias size = list(out.shape[1:]) if not self._built: - self._b = self._layer.create_parameter( + self._b = self._helper.create_parameter( attr=self._bias_attr, shape=size, dtype=out.dtype, is_bias=True) - bias_out = self.create_variable_for_type_inference(dtype=out.dtype) - self.append_op( + bias_out = self._helper.create_variable_for_type_inference( + dtype=out.dtype) + self._helper.append_op( type='elementwise_add', inputs={'X': [out], 'Y': [self._b]}, diff --git a/python/paddle/fluid/layer_helper.py b/python/paddle/fluid/layer_helper.py index ea9953f5814..e0fd44ae31e 100644 --- a/python/paddle/fluid/layer_helper.py +++ b/python/paddle/fluid/layer_helper.py @@ -405,8 +405,7 @@ class LayerHelper(object): """ size = list(input_var.shape[dim_start:dim_end]) bias_attr = self.bias_attr - if not bias_attr: - return input_var + assert bias_attr is not None b = self.create_parameter( attr=bias_attr, shape=size, dtype=input_var.dtype, is_bias=True) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_gan.py b/python/paddle/fluid/tests/unittests/test_imperative_gan.py index c38906ce6ad..410c75026bb 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_gan.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_gan.py @@ -121,21 +121,21 @@ class TestImperativeMnist(unittest.TestCase): img = np.ones([2, 1], np.float32) noise = np.ones([2, 2], np.float32) exe.run(startup) - d_loss_val = exe.run(discriminate_p, - feed={'img': img, - 'noise': noise}, - fetch_list=[d_loss])[0] - g_loss_val = exe.run(generate_p, - feed={'noise': noise}, - fetch_list=[g_loss])[0] + static_d_loss = exe.run(discriminate_p, + feed={'img': img, + 'noise': noise}, + fetch_list=[d_loss])[0] + static_g_loss = exe.run(generate_p, + feed={'noise': noise}, + fetch_list=[g_loss])[0] + + # generate_p contains all parameters needed. for param in generate_p.global_block().all_parameters(): static_params[param.name] = np.array( scope.find_var(param.name).get_tensor()) sys.stderr.write( 'static_param_loss: %s: %s\n' % (param.name, np.sum(static_params[param.name]))) - sys.stderr.write('d_loss %s, g_loss: %s\n' % - (d_loss_val, g_loss_val)) dy_params = dict() with fluid.imperative.guard(): @@ -181,8 +181,14 @@ class TestImperativeMnist(unittest.TestCase): dy_params[p.name] = p._numpy() sys.stderr.write('dy_param_loss: %s: %s\n' % (p.name, np.sum(dy_params[p.name]))) - sys.stderr.write('dy_d_loss: %s, dy_g_loss: %s\n' % - (d_loss._numpy(), g_loss._numpy())) + + dy_g_loss = g_loss._numpy() + dy_d_loss = d_loss._numpy() + + self.assertEqual(dy_g_loss, static_g_loss) + self.assertEqual(dy_d_loss, static_d_loss) + for k, v in six.iteritems(dy_params): + self.assertTrue(np.allclose(v, static_params[k])) if __name__ == '__main__': -- GitLab