param_attr.py 7.0 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
F
fengjiayi 已提交
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
#
F
fengjiayi 已提交
9 10 11 12 13
# 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.
F
update  
fengjiayi 已提交
14

15 16
import six

17 18
from .initializer import Initializer, Xavier, Constant
from .regularizer import WeightDecayRegularizer
Y
Yu Yang 已提交
19

20 21 22 23
__all__ = [
    'ParamAttr',
    'WeightNormParamAttr',
]
Y
Yu Yang 已提交
24

Y
Yu Yang 已提交
25 26

class ParamAttr(object):
C
chengduoZH 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    """
    Parameter attributes object. To fine-tuning network training process, user
    can set parameter's attributes to control training details. Such as learning rate,
    regularization, trainable, do_model_average and the method to initialize param.


    Args:
        name(str): The parameter's name. Default None.
        initializer(Initializer): The method to initial this parameter. Default None.
        learning_rate(float): The parameter's learning rate. The learning rate when
            optimize is :math:`global\_lr * parameter\_lr * scheduler\_factor`.
            Default 1.0.
        regularizer(WeightDecayRegularizer): Regularization factor. Default None.
        trainable(bool): Whether this parameter is trainable. Default True.
        gradient_clip(BaseGradientClipAttr): The method to clip this parameter's
            gradient. Default None.
        do_model_average(bool): Whether this parameter should do model average.
            Default False.

    Examples:
        .. code-block:: python

            w_param_attrs = fluid.ParamAttr(name="fc_weight",
                                            learning_rate=0.5,
                                            regularizer=fluid.L2Decay(1.0),
                                            trainable=True)
            y_predict = fluid.layers.fc(input=x, size=10, param_attr=w_param_attrs)
    """

Y
Yu Yang 已提交
56 57 58 59 60
    def __init__(self,
                 name=None,
                 initializer=None,
                 learning_rate=1.0,
                 regularizer=None,
Y
Yu Yang 已提交
61
                 trainable=True,
W
wanghaoshuang 已提交
62
                 gradient_clip=None,
C
chengduoZH 已提交
63
                 do_model_average=False):
Y
Yu Yang 已提交
64 65 66 67 68
        self.name = name
        self.initializer = initializer
        self.learning_rate = learning_rate
        self.regularizer = regularizer
        self.trainable = trainable
F
fengjiayi 已提交
69
        self.gradient_clip = gradient_clip
W
wanghaoshuang 已提交
70
        self.model_average = do_model_average
Y
Yu Yang 已提交
71

Y
yuyang18 已提交
72
    def _set_default_initializer(self, initializer):
C
chengduoZH 已提交
73 74 75
        """
        Set the default initializer, the initializer should be Constant,
        Uniform, Normal, Xavier, MSRA.
C
chengduoZH 已提交
76 77 78 79 80 81

        Args:
            initializer(Initializer): the initializer to set.

        Returns:
            None
C
chengduoZH 已提交
82
        """
Y
Yu Yang 已提交
83 84 85 86 87 88 89 90 91 92
        if initializer is None:
            if self.initializer is None:
                raise ValueError("ParamAttr.initializer is not set")
            return

        if self.initializer is not None:
            return

        self.initializer = initializer

Y
yuyang18 已提交
93
    def _set_default_param_initializer(self):
C
chengduoZH 已提交
94 95
        """
        Set the default initializer for the parameter with Xavier.
C
chengduoZH 已提交
96 97 98 99 100 101

        Args:
            None.

        Returns:
            None.
C
chengduoZH 已提交
102
        """
Y
yuyang18 已提交
103
        self._set_default_initializer(Xavier())
Y
Yu Yang 已提交
104

Y
yuyang18 已提交
105
    def _set_default_bias_initializer(self):
C
chengduoZH 已提交
106 107
        """
        Set the default initializer for the bias with Constant(0.0).
C
chengduoZH 已提交
108 109 110 111 112 113

        Args:
            None.

        Returns:
            None.
C
chengduoZH 已提交
114
        """
Y
yuyang18 已提交
115
        self._set_default_initializer(Constant(0.0))
Y
Yu Yang 已提交
116 117

    @staticmethod
Y
yuyang18 已提交
118
    def _to_attr(arg):
C
chengduoZH 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132
        """
        Create ParamAttr[s].

        Args:
            arg: Arguments to initialize ParamAttr[s]. arg's type can be
                str, Initializer, float, WeightDecayRegularizer, BaseGradientClipAttr,
                bool, ParamAttr, or a list of above type.

        Returns:
            ParamAttr[s]: ParamAttr[s] initialized with arg.

        Raises:
            arg can not initialize a ParamAttr.
        """
Y
Yu Yang 已提交
133 134
        if arg is None:
            return ParamAttr()
135
        elif isinstance(arg, list) or isinstance(arg, tuple):
Y
yuyang18 已提交
136
            return [ParamAttr._to_attr(a) for a in arg]
Y
Yu Yang 已提交
137 138
        elif isinstance(arg, ParamAttr):
            return arg
139
        elif isinstance(arg, six.string_types):
Y
Yu Yang 已提交
140 141 142 143 144 145
            return ParamAttr(name=arg)
        elif isinstance(arg, Initializer):
            return ParamAttr(initializer=arg)
        elif isinstance(arg, WeightDecayRegularizer):
            return ParamAttr(regularizer=arg)
        elif isinstance(arg, bool):
Y
yuyang18 已提交
146
            return ParamAttr._to_attr(None) if arg else False
Y
Yu Yang 已提交
147 148 149
        else:
            raise TypeError("{0} cast to ParamAttr".format(type(arg)))

Y
yuyang18 已提交
150
    def _to_kwargs(self, with_initializer=False):
C
chengduoZH 已提交
151 152 153 154 155 156 157 158 159
        """
        Returns the attributes of this parameter.

        Args:
            with_initializer(bool): Whether to add initializer attr.

        Returns:
            Parameter attributes(map): The attributes of this parameter.
        """
Y
Yu Yang 已提交
160 161
        kwargs = {
            'name': self.name,
G
guosheng 已提交
162 163 164
            'optimize_attr': {
                'learning_rate': self.learning_rate
            },
Y
Yu Yang 已提交
165
            'regularizer': self.regularizer,
Y
Yu Yang 已提交
166
            'trainable': self.trainable,
W
wanghaoshuang 已提交
167
            'gradient_clip_attr': self.gradient_clip,
W
wanghaoshuang 已提交
168
            'model_average': self.model_average
Y
Yu Yang 已提交
169 170 171 172
        }
        if with_initializer:
            kwargs['initializer'] = self.initializer
        return kwargs
G
guosheng 已提交
173 174 175 176


class WeightNormParamAttr(ParamAttr):
    """
C
chengduoZH 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    Used for weight Norm. Weight Norm is a reparameterization of the weight vectors
    in a neural network that decouples the length of those weight vectors from
    their direction. Weight Norm has been implemented as discussed in this
    paper: `Weight Normalization: A Simple Reparameterization to Accelerate
    Training of Deep Neural Networks
    <https://arxiv.org/pdf/1602.07868.pdf>`_.

    Args:
        dim(list): The parameter's name. Default None.
        kwargs: Any field in ParamAttr. Default None.

    Examples:
        .. code-block:: python

            data = fluid.layers.data(name="data", shape=[3, 32, 32], dtype="float32")
            fc = fluid.layers.fc(input=data,
                                 size=1000,
                                 param_attr=WeightNormParamAttr(
                                      dim=None,
                                      name='weight_norm_param'))

G
guosheng 已提交
198 199 200
    """
    # List to record the parameters reparameterized by weight normalization.
    # If these parameters are treated as Variable rather than Parameter,
201
    # it can be used to discriminate these parameters and help to serialize
G
guosheng 已提交
202 203 204 205 206 207
    # these paramters for inference.
    params_with_weight_norm = []

    def __init__(self, dim=None, **kwargs):
        super(WeightNormParamAttr, self).__init__(**kwargs)
        self.dim = dim