layer_helper.py 6.7 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15 16
from __future__ import print_function

Y
Yu Yang 已提交
17
import copy
18
import six
Y
Yu Yang 已提交
19

L
lujun 已提交
20
from .framework import Parameter, dtype_is_floating, _in_dygraph_mode
21
from . import unique_name
22
from paddle.fluid.initializer import Constant, Xavier
23
from .param_attr import ParamAttr
24
from . import core
25
from six.moves import zip
26
from .layer_helper_base import LayerHelperBase
Y
Yu Yang 已提交
27

Y
Yu Yang 已提交
28

29
class LayerHelper(LayerHelperBase):
Y
Yu Yang 已提交
30 31 32
    def __init__(self, layer_type, **kwargs):
        self.kwargs = kwargs
        name = self.kwargs.get('name', None)
L
lujun 已提交
33
        # TODO(panyx0718, minqiyang): dygraph mode
X
Xin Pan 已提交
34
        # can not use both `layer_type` and `name`. Deprecate LayerHelper
L
lujun 已提交
35
        # and write a Helper for dygraph mode.
Y
Yu Yang 已提交
36
        if name is None:
37
            self.kwargs['name'] = unique_name.generate(layer_type)
Y
Yu Yang 已提交
38

39 40
        super(LayerHelper, self).__init__(
            self.kwargs['name'], layer_type=layer_type)
41

Y
Yu Yang 已提交
42
    def append_op(self, *args, **kwargs):
43
        return self.main_program.current_block().append_op(*args, **kwargs)
Y
Yu Yang 已提交
44 45 46

    def multiple_input(self, input_param_name='input'):
        inputs = self.kwargs.get(input_param_name, [])
47 48 49 50
        ret = []
        if isinstance(inputs, list) or isinstance(inputs, tuple):
            for inp in inputs:
                ret.append(self.to_variable(inp))
Y
Yu Yang 已提交
51
        else:
52 53
            ret.append(self.to_variable(inputs))
        return ret
Y
Yu Yang 已提交
54 55 56 57 58 59 60 61 62

    def input(self, input_param_name='input'):
        inputs = self.multiple_input(input_param_name)
        if len(inputs) != 1:
            raise "{0} layer only takes one input".format(self.layer_type)
        return inputs[0]

    @property
    def param_attr(self):
Y
yuyang18 已提交
63
        return ParamAttr._to_attr(self.kwargs.get('param_attr', None))
Y
Yu Yang 已提交
64

Q
QI JUN 已提交
65
    @property
Q
QI JUN 已提交
66
    def bias_attr(self):
Y
yuyang18 已提交
67
        return ParamAttr._to_attr(self.kwargs.get('bias_attr', None))
Y
Yu Yang 已提交
68

69
    #TODO (jiabin): reconstruct this in LayerObjHelper and avoid dependency of param_attr
Y
Yu Yang 已提交
70 71
    def multiple_param_attr(self, length):
        param_attr = self.param_attr
Y
Yu Yang 已提交
72
        if isinstance(param_attr, ParamAttr):
Y
Yu Yang 已提交
73 74 75 76 77 78
            param_attr = [param_attr]

        if len(param_attr) != 1 and len(param_attr) != length:
            raise ValueError("parameter number mismatch")
        elif len(param_attr) == 1 and length != 1:
            tmp = [None] * length
M
minqiyang 已提交
79
            for i in six.moves.range(length):
Y
Yu Yang 已提交
80 81 82 83 84 85 86
                tmp[i] = copy.deepcopy(param_attr[0])
            param_attr = tmp
        return param_attr

    def iter_inputs_and_params(self, input_param_name='input'):
        inputs = self.multiple_input(input_param_name)
        param_attrs = self.multiple_param_attr(len(inputs))
87
        for ipt, param_attr in zip(inputs, param_attrs):
Y
Yu Yang 已提交
88 89 90 91 92 93 94
            yield ipt, param_attr

    def input_dtype(self, input_param_name='input'):
        inputs = self.multiple_input(input_param_name)
        dtype = None
        for each in inputs:
            if dtype is None:
F
fengjiayi 已提交
95 96
                dtype = each.dtype
            elif dtype != each.dtype:
Q
Qiao Longfei 已提交
97 98
                raise ValueError("Data Type mismatch: %d to %d" %
                                 (dtype, each.dtype))
Y
Yu Yang 已提交
99 100
        return dtype

Q
Qiao Longfei 已提交
101 102 103 104 105 106
    def get_parameter(self, name):
        param = self.main_program.global_block().var(name)
        if not isinstance(param, Parameter):
            raise ValueError("no Parameter name %s found" % name)
        return param

107
    #TODO (jiabin): reconstruct this in LayerObjHelper and avoid dependency of bias_attr
Y
Yu Yang 已提交
108
    def append_bias_op(self, input_var, dim_start=1, dim_end=None):
109
        """
X
xuwei06 已提交
110
        Append bias operator and return its output. If the user does not set
111
        bias_attr, append_bias_op will return input_var
X
xuwei06 已提交
112

113 114 115 116
        :param input_var: the input variable. The len(input_var.shape) is
        larger or equal than 2.
        :bias_initializer: an instance of a subclass of Initializer used to
        initialize the bias
X
xuwei06 已提交
117 118
        :param dim_start:
        :param dim_end: the shape of the bias will be
X
xuwei06 已提交
119
        input_var.shape[dim_start:dim_end]. The bias is broadcasted to other
X
xuwei06 已提交
120
        dimensions and added to input_var to get the output
121
        """
X
xuwei06 已提交
122
        size = list(input_var.shape[dim_start:dim_end])
Q
QI JUN 已提交
123
        bias_attr = self.bias_attr
Y
Yu Yang 已提交
124 125
        if not bias_attr:
            return input_var
126

Y
Yu Yang 已提交
127
        b = self.create_parameter(
Y
Yu Yang 已提交
128
            attr=bias_attr, shape=size, dtype=input_var.dtype, is_bias=True)
X
Xin Pan 已提交
129
        tmp = self.create_variable_for_type_inference(dtype=input_var.dtype)
Y
Yu Yang 已提交
130 131 132 133
        self.append_op(
            type='elementwise_add',
            inputs={'X': [input_var],
                    'Y': [b]},
X
xuwei06 已提交
134 135
            outputs={'Out': [tmp]},
            attrs={'axis': dim_start})
Y
Yu Yang 已提交
136 137
        return tmp

138
    #TODO (jiabin): reconstruct this in LayerObjHelper and avoid dependency of act
M
minqiyang 已提交
139
    def append_activation(self, input_var):
Y
Yu Yang 已提交
140 141 142
        act = self.kwargs.get('act', None)
        if act is None:
            return input_var
143
        if isinstance(act, six.string_types):
Y
Yu Yang 已提交
144
            act = {'type': act}
M
minqiyang 已提交
145 146
        else:
            raise TypeError(str(act) + " should be unicode or str")
147

K
Kexin Zhao 已提交
148 149
        if 'use_cudnn' in self.kwargs and self.kwargs.get('use_cudnn'):
            act['use_cudnn'] = self.kwargs.get('use_cudnn')
150 151
        if 'use_mkldnn' in self.kwargs:
            act['use_mkldnn'] = self.kwargs.get('use_mkldnn')
Y
Yu Yang 已提交
152
        act_type = act.pop('type')
153

L
liuwei1031 已提交
154
        tmp = self.create_variable_for_type_inference(dtype=input_var.dtype)
Y
Yu Yang 已提交
155 156 157
        self.append_op(
            type=act_type,
            inputs={"X": [input_var]},
158
            outputs={"Out": [tmp]},
Y
Yu Yang 已提交
159
            attrs=act)
160
        return tmp
161

162
    #TODO (jiabin): should we remove this since it has never be used
163 164
    def _get_default_initializer(self, dtype):
        if dtype is None or dtype_is_floating(dtype) is True:
165
            return Xavier()
166 167
        else:
            # For integer and boolean types, initialize with all zeros
168
            return Constant()
Y
Yang Yu 已提交
169

170
    #TODO (jiabin): reconstruct this in LayerObjHelper and avoid dependency of kwargs
Y
Yang Yu 已提交
171 172 173 174 175
    def is_instance(self, param_name, cls):
        param = self.kwargs.get(param_name, None)
        if not isinstance(param, cls):
            raise TypeError("The input {0} parameter of method {1} must be {2}",
                            param_name, self.layer_type, cls.__name__)