From 95967313484fb0e105d9ff413887f390f3ca938d Mon Sep 17 00:00:00 2001 From: Yu Yang Date: Thu, 26 Oct 2017 17:27:21 -0700 Subject: [PATCH] enhance default param_attrs (#5142) --- python/paddle/v2/framework/layer_helper.py | 27 +++++++++++++------ .../paddle/v2/framework/tests/test_layers.py | 18 +++---------- .../v2/framework/tests/test_word2vec.py | 18 +++---------- 3 files changed, 27 insertions(+), 36 deletions(-) diff --git a/python/paddle/v2/framework/layer_helper.py b/python/paddle/v2/framework/layer_helper.py index f3da32f0e..6142b1f93 100644 --- a/python/paddle/v2/framework/layer_helper.py +++ b/python/paddle/v2/framework/layer_helper.py @@ -75,18 +75,29 @@ class LayerHelper(object): } } actual = self.kwargs.get('param_attr', None) - return actual if actual is not None else default + if actual is None: + actual = default + for default_field in default.keys(): + if default_field not in actual: + actual[default_field] = default[default_field] + return actual def bias_attr(self): + default = { + 'name': None, + 'init_attr': { + 'type': 'fill_constant', + 'value': 0.0 + } + } bias_attr = self.kwargs.get('bias_attr', None) if bias_attr is True: - bias_attr = { - 'name': None, - 'init_attr': { - 'type': 'fill_constant', - 'value': 0.0 - } - } + bias_attr = default + + if isinstance(bias_attr, dict): + for default_field in default.keys(): + if default_field not in bias_attr: + bias_attr[default_field] = default[default_field] return bias_attr def multiple_param_attr(self, length): diff --git a/python/paddle/v2/framework/tests/test_layers.py b/python/paddle/v2/framework/tests/test_layers.py index 7aedb985f..54f8a0270 100644 --- a/python/paddle/v2/framework/tests/test_layers.py +++ b/python/paddle/v2/framework/tests/test_layers.py @@ -103,40 +103,30 @@ class TestBook(unittest.TestCase): next_word = layers.data( name='nextw', shape=[1], data_type='int32', program=program) - embed_param_attr_1 = { - 'name': 'shared_w', - 'init_attr': { - 'max': 1.0, - 'type': 'uniform_random', - 'min': -1.0 - } - } - embed_param_attr_2 = {'name': 'shared_w'} - embed_first = layers.embedding( input=first_word, size=[dict_size, embed_size], data_type='float32', - param_attr=embed_param_attr_1, + param_attr={'name': 'shared_w'}, program=program) embed_second = layers.embedding( input=second_word, size=[dict_size, embed_size], data_type='float32', - param_attr=embed_param_attr_2, + param_attr={'name': 'shared_w'}, program=program) embed_third = layers.embedding( input=third_word, size=[dict_size, embed_size], data_type='float32', - param_attr=embed_param_attr_2, + param_attr={'name': 'shared_w'}, program=program) embed_forth = layers.embedding( input=forth_word, size=[dict_size, embed_size], data_type='float32', - param_attr=embed_param_attr_2, + param_attr={'name': 'shared_w'}, program=program) concat_embed = layers.concat( diff --git a/python/paddle/v2/framework/tests/test_word2vec.py b/python/paddle/v2/framework/tests/test_word2vec.py index b5d980351..f5e61bef0 100644 --- a/python/paddle/v2/framework/tests/test_word2vec.py +++ b/python/paddle/v2/framework/tests/test_word2vec.py @@ -50,28 +50,18 @@ next_word = layers.data( program=program, init_program=init_program) -embed_param_attr_1 = { - 'name': 'shared_w', - 'init_attr': { - 'max': 1.0, - 'type': 'uniform_random', - 'min': -1.0 - } -} -embed_param_attr_2 = {'name': 'shared_w'} - embed_first = layers.embedding( input=first_word, size=[dict_size, embed_size], data_type='float32', - param_attr=embed_param_attr_1, + param_attr={'name': 'shared_w'}, program=program, init_program=init_program) embed_second = layers.embedding( input=second_word, size=[dict_size, embed_size], data_type='float32', - param_attr=embed_param_attr_2, + param_attr={'name': 'shared_w'}, program=program, init_program=init_program) @@ -79,14 +69,14 @@ embed_third = layers.embedding( input=third_word, size=[dict_size, embed_size], data_type='float32', - param_attr=embed_param_attr_2, + param_attr={'name': 'shared_w'}, program=program, init_program=init_program) embed_forth = layers.embedding( input=forth_word, size=[dict_size, embed_size], data_type='float32', - param_attr=embed_param_attr_2, + param_attr={'name': 'shared_w'}, program=program, init_program=init_program) -- GitLab