attrs.py 10.5 KB
Newer Older
1
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#
# 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
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# 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.

from paddle.trainer.config_parser import *
Q
qijun 已提交
16
__all__ = [
X
xzl 已提交
17 18
    'HookAttr', 'ParamAttr', 'ExtraAttr', 'ParameterAttribute',
    'ExtraLayerAttribute'
Q
qijun 已提交
19
]
Z
zhangjinchao01 已提交
20 21


22
def convert_and_compare(x, Type):
W
wangyanfei01 已提交
23 24 25 26 27 28
    """
    Convert x to be the same type as Type and then convert back to
    check whether there is a loss of information
    :param x: object to be checked
    :param Type: target type to check x over

29
    """
Q
qijun 已提交
30 31
    return type(x)(Type(x)) == x

32 33

def is_compatible_with(x, Type):
W
wangyanfei01 已提交
34 35 36 37 38
    """
    Check if x has a type compatible with Type
    :param x: object to be checked
    :param Type: target type to check x over

39 40 41 42 43
    """
    if type(x) == Type:
        return True
    try:
        if float == Type or int == Type:
W
wangyanfei01 已提交
44 45 46
            # avoid those types that can be converted to float/int but not very
            # meaningful and  could potentially lead to error
            # i.e., str and bool typed value should not be used for initializing float/int variable
47 48 49
            if not isinstance(x, str) and not isinstance(x, bool):
                return convert_and_compare(x, Type)
        elif bool == Type:
W
wangyanfei01 已提交
50
            # should not use string type to initialize bool variable
51 52 53 54 55 56 57 58
            if not isinstance(x, str):
                return convert_and_compare(x, Type)
        else:
            return False
    except:
        return False


X
xzl 已提交
59 60 61
class HookAttribute(object):
    """
    Hook Attribute object. The hook is an auxiliary operation that occurs 
X
xzl 已提交
62
    during network propagation.
X
xzl 已提交
63
    NOTE: IT IS A HIGH LEVEL USER INTERFACE.
X
xzl 已提交
64

65
    :param  type: Hook type, eg: 'pruning'
X
xzl 已提交
66 67
    :type type: string

X
xzl 已提交
68 69
    :param sparsity_ratio: Must be specified if hook type is 'pruning'
    :type sparsity_ratio: float or None
X
xzl 已提交
70 71 72
	
    """

73
    def __init__(self, type, sparsity_ratio=None):
X
xzl 已提交
74 75
        self.type = type
        self.sparsity_ratio = sparsity_ratio
X
xzl 已提交
76 77 78 79 80
        if self.sparsity_ratio is not None:
            assert is_compatible_with(
                self.sparsity_ratio,
                float), 'sparisity_ratio must be float type'
            assert self.sparsity_ratio <= 1 and self.sparsity_ratio >= 0, 'sparisity must be a flaot between [0, 1] '
X
xzl 已提交
81 82

    def __call__(self):
83
        return ParameterHook(self.type, sparsity_ratio=self.sparsity_ratio)
X
xzl 已提交
84 85


Z
zhangjinchao01 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
class ParameterAttribute(object):
    """
    Parameter Attributes object. To fine-tuning network training process, user
    can set attribute to control training details, such as l1,l2 rate / learning
    rate / how to init param.

    NOTE: IT IS A HIGH LEVEL USER INTERFACE.

    :param is_static: True if this parameter will be fixed while training.
    :type is_static: bool

    :param initial_std: Gauss Random initialization standard deviation.
                        None if not using Gauss Random initialize parameter.
    :type initial_std: float or None
    :param initial_mean:  Gauss Random initialization mean.
                         None if not using Gauss Random initialize parameter.
    :type initial_mean: float or None
    :param initial_max: Uniform initialization max value.
    :type initial_max: float or None
    :param initial_min: Uniform initialization min value.
    :type initial_min: float or None
    :param l1_rate: the l1 regularization factor
    :type l1_rate: float or None
    :param l2_rate: the l2 regularization factor
    :type l2_rate: float or None
    :param learning_rate: The parameter learning rate. None means 1.
                          The learning rate when optimize is LEARNING_RATE =
                          GLOBAL_LEARNING_RATE * PARAMETER_LEARNING_RATE
                          * SCHEDULER_FACTOR.

    :type learning_rate: float or None
    :param momentum: The parameter momentum. None means use global value.
    :type momentum: float or None
W
wangyanfei01 已提交
119 120 121 122
    :param gradient_clipping_threshold: gradient clipping threshold. If gradient
                                        value larger than some value, will be
                                        clipped.
    :type gradient_clipping_threshold: float
Z
zhangjinchao01 已提交
123 124 125
    :param sparse_update: Enable sparse update for this parameter. It will
                          enable both local and remote sparse update.
    :type sparse_update: bool
X
xuwei06 已提交
126 127 128 129
    :param initializer: If not None, it should be a callable object which accepts
                        a parameter name and returns numpy array for the initial
                        value of the parameter
    :param initializer: callable object
Z
zhangjinchao01 已提交
130 131
    """

Q
qijun 已提交
132 133 134 135 136 137 138 139 140 141 142
    def __init__(self,
                 name=None,
                 is_static=False,
                 initial_std=None,
                 initial_mean=None,
                 initial_max=None,
                 initial_min=None,
                 l1_rate=None,
                 l2_rate=None,
                 learning_rate=None,
                 momentum=None,
W
wangyanfei01 已提交
143
                 gradient_clipping_threshold=None,
X
xzl 已提交
144
                 sparse_update=False,
Z
zlx 已提交
145
                 update_hooks=None,
X
xuwei06 已提交
146
                 initializer=None):
147 148
        self.attr = {}

Z
zhangjinchao01 已提交
149
        if is_static:
150 151 152
            self.attr['is_static'] = True

        if initial_std is None and initial_mean is None and initial_max \
Z
zhangjinchao01 已提交
153
                is None and initial_min is None:
154
            self.attr['initial_smart'] = True
155 156
        elif is_compatible_with(initial_std, float) or \
             is_compatible_with(initial_mean, float):
Z
zhangjinchao01 已提交
157 158 159 160 161
            if initial_std is not None:
                self.attr['initial_std'] = initial_std
            if initial_mean is not None:
                self.attr['initial_mean'] = initial_mean
            self.attr['initial_strategy'] = 0  # Gauss Random
162 163 164 165
        elif is_compatible_with(initial_max, float) and \
             is_compatible_with(initial_min, float):
            initial_max = initial_max
            initial_min = initial_min
Z
zhangjinchao01 已提交
166 167 168 169 170 171 172 173 174
            assert initial_min < initial_max
            initial_mean = (initial_max + initial_min) / 2
            initial_std = initial_mean - initial_min
            self.attr['initial_mean'] = initial_mean
            self.attr['initial_std'] = initial_std
            self.attr['initial_strategy'] = 1  # Uniform Random
        else:
            raise RuntimeError("Unexpected branch.")

175
        if not is_static and is_compatible_with(l1_rate, float):
Z
zhangjinchao01 已提交
176 177
            self.attr['decay_rate_l1'] = l1_rate

178
        if not is_static and is_compatible_with(l2_rate, float):
Z
zhangjinchao01 已提交
179 180
            self.attr['decay_rate'] = l2_rate

181
        if not is_static and is_compatible_with(learning_rate, float):
Z
zhangjinchao01 已提交
182 183
            self.attr['learning_rate'] = learning_rate

184
        if not is_static and is_compatible_with(momentum, float):
Z
zhangjinchao01 已提交
185 186 187 188 189 190 191 192 193
            self.attr['momentum'] = momentum

        if name is not None:
            self.attr['parameter_name'] = name

        if sparse_update:
            self.attr['sparse_update'] = True
            self.attr['sparse_remote_update'] = True

W
wangyanfei01 已提交
194 195 196 197
        if gradient_clipping_threshold is not None and \
                is_compatible_with(gradient_clipping_threshold, float):
            self.attr['gradient_clipping_threshold'] = \
                gradient_clipping_threshold
X
xuwei06 已提交
198 199
        if initializer is not None:
            self.attr['initializer'] = initializer
W
wangyanfei01 已提交
200

X
xzl 已提交
201 202 203
        if update_hooks:
            self.attr['update_hooks'] = update_hooks

Z
zhangjinchao01 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    def set_default_parameter_name(self, name):
        """
        Set default parameter name. If parameter not set, then will use default
        parameter name.


        :param name: default parameter name.
        :type name: basestring
        """
        if 'parameter_name' not in self.attr:
            self.attr['parameter_name'] = name

    @staticmethod
    def to_bias(bias_attr):
        if isinstance(bias_attr, ParameterAttribute):
            return Bias(**bias_attr.attr)
        else:
            return False


class ExtraLayerAttribute(object):
    """
    Some high level layer attributes config. You can set all attributes here,
    but some layer doesn't support all attributes. If you set an attribute to a
    layer that not support this attribute, paddle will print an error and core.

    :param error_clipping_threshold: Error clipping threshold.
    :type error_clipping_threshold: float
    :param drop_rate: Dropout rate. Dropout will create a mask on layer output.
                      The dropout rate is the zero rate of this mask. The
                      details of what dropout is please refer to `here
                      <https://www.cs.toronto.edu/~hinton/absps/
236
                      JMLRdropout.pdf>`_.
Z
zhangjinchao01 已提交
237
    :type drop_rate: float
P
Peng Li 已提交
238
    :param device: device ID of layer. device=-1, use CPU. device>=0, use GPU.
239 240 241 242
                   The details allocation in parallel_nn please refer to `here
                   <http://www.paddlepaddle.org/doc/ui/cmd_argument/
                   use_case.html#case-2-specify-layers-in-different-devices>`_.
    :type device: int
Z
zhangjinchao01 已提交
243 244
    """

Q
qijun 已提交
245 246 247 248
    def __init__(self,
                 error_clipping_threshold=None,
                 drop_rate=None,
                 device=None):
Z
zhangjinchao01 已提交
249
        self.attr = dict()
Y
Yu Yang 已提交
250 251 252 253 254 255 256 257 258
        if error_clipping_threshold is not None:
            error_clipping_threshold = float(error_clipping_threshold)
            if error_clipping_threshold < 0:
                raise ValueError("Error clipping must > 0")
            self.attr['error_clipping_threshold'] = error_clipping_threshold
        if drop_rate is not None:
            drop_rate = float(drop_rate)
            if drop_rate < 0:
                raise ValueError("Dropout rate must > 0")
Z
zhangjinchao01 已提交
259 260
            self.attr["drop_rate"] = drop_rate

261 262 263
        if isinstance(device, int):
            self.attr["device"] = device

Z
zhangjinchao01 已提交
264 265 266 267
    def check(self, layer_name):
        for key in self.attr:
            if not hasattr(self, 'can_%s' % key) or \
                    not getattr(self, 'can_%s' % key):
Q
qijun 已提交
268 269
                raise NotImplementedError("Layer %s cannot support %s" %
                                          (layer_name, key))
Z
zhangjinchao01 已提交
270 271 272 273 274 275 276 277 278

    @staticmethod
    def to_kwargs(attr):
        if attr is None:
            return dict()
        else:
            return attr.attr


X
xzl 已提交
279
HookAttr = HookAttribute
Z
zhangjinchao01 已提交
280 281
ParamAttr = ParameterAttribute
ExtraAttr = ExtraLayerAttribute