From f354bd98610f184a11f22235d434ceb7bef3811e Mon Sep 17 00:00:00 2001 From: Yu Yang Date: Tue, 31 Oct 2017 12:03:07 -0700 Subject: [PATCH] AddBiasOp does not care num_flatten_dims (#5200) * AddBiasOp does not care num_flatten_dims * Add comments --- python/paddle/v2/framework/layer_helper.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/python/paddle/v2/framework/layer_helper.py b/python/paddle/v2/framework/layer_helper.py index c57776441..45d9cf3f4 100644 --- a/python/paddle/v2/framework/layer_helper.py +++ b/python/paddle/v2/framework/layer_helper.py @@ -142,8 +142,24 @@ class LayerHelper(object): return self.program.global_block().create_var( *args, persistable=False, **kwargs) - def append_bias_op(self, input_var): - size = list(input_var.shape[1:]) + def append_bias_op(self, input_var, num_flatten_dims=None): + """ + Append bias operator and return its output. If the user does not set + bias_attr, append_bias_op will return input_var + + :param input_var: the input variable. The len(input_var.shape) is larger + or equal than 2. + :param num_flatten_dims: The input tensor will be flatten as a matrix + when adding bias. + `matrix.shape = product(input_var.shape[0:num_flatten_dims]), product( + input_var.shape[num_flatten_dims:])` + """ + if num_flatten_dims is None: + num_flatten_dims = self.kwargs.get('num_flatten_dims', None) + if num_flatten_dims is None: + num_flatten_dims = 1 + + size = list(input_var.shape[num_flatten_dims:]) bias_attr = self.bias_attr() if not bias_attr: return input_var -- GitLab