optimizer.py 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
#
# 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.
Q
qiaolongfei 已提交
14

15 16 17 18
import paddle.trainer_config_helpers.config_parser_utils as config_parser_utils
import paddle.trainer_config_helpers.optimizers as v1_optimizers
from paddle.proto.OptimizerConfig_pb2 import OptimizerConfig

L
Luo Tao 已提交
19 20 21 22
__all__ = [
    'Momentum', 'Adam', 'Adamax', 'AdaGrad', 'DecayedAdaGrad', 'AdaDelta',
    'RMSProp', 'ModelAverage', 'L2Regularization'
]
Q
qiaolongfei 已提交
23 24 25 26


class Optimizer(object):
    def __init__(self, **kwargs):
Y
Yu Yang 已提交
27
        import py_paddle.swig_paddle as swig_api
Q
qiaolongfei 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
        if 'batch_size' in kwargs:
            del kwargs['batch_size']  # not important for python library.

        def __impl__():
            v1_optimizers.settings(batch_size=1, **kwargs)

        self.__opt_conf_proto__ = config_parser_utils.parse_optimizer_config(
            __impl__)
        self.__opt_conf__ = swig_api.OptimizationConfig.createFromProto(
            self.__opt_conf_proto__)

    def enable_types(self):
        """
        get enable_types for each optimizer.
        enable_types = [value, gradient, momentum, etc]
        For each optimizer(SGD, Adam), GradientMachine should enable different
        buffers.
        """
Y
Yu Yang 已提交
46
        import py_paddle.swig_paddle as swig_api
Q
qiaolongfei 已提交
47 48 49 50
        tmp = swig_api.ParameterOptimizer.create(self.__opt_conf__)
        assert isinstance(tmp, swig_api.ParameterOptimizer)
        return tmp.getParameterTypes()

Q
qiaolongfei 已提交
51
    def __create_local_updater__(self):
Y
Yu Yang 已提交
52
        import py_paddle.swig_paddle as swig_api
Q
qiaolongfei 已提交
53 54
        return swig_api.ParameterUpdater.createLocalUpdater(self.__opt_conf__)

Q
qiaolongfei 已提交
55
    def __create_remote_updater__(self, pass_num, use_sparse_updater):
Y
Yu Yang 已提交
56
        import py_paddle.swig_paddle as swig_api
Q
qiaolongfei 已提交
57 58
        return swig_api.ParameterUpdater.createRemoteUpdater(
            self.__opt_conf__, pass_num, use_sparse_updater)
Q
qiaolongfei 已提交
59

60
    def __create_new_remote_updater__(self, pserver_spec, use_etcd):
Y
Yu Yang 已提交
61
        import py_paddle.swig_paddle as swig_api
62
        return swig_api.ParameterUpdater.createNewRemoteUpdater(
63
            self.__opt_conf__, pserver_spec, use_etcd)
64 65

    def create_updater(self, is_local, num_passes, use_sparse_updater,
66
                       pserver_spec, use_etcd):
Q
qiaolongfei 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79
        """
        create proper parameter_updater by configuration.
        :param is_local: create local or remote parameter updater
        :param num_passes: remote parameter updater will use this to config
        parameter server.
        :param use_sparse_updater: when use remote updater, if some parameter is
        sparse, updater should do some extra thing:

        ..  code-block:: python

            if use_sparse_remote_updater:
                        gradient_machine.prefetch(in_args)
                        parameter_updater.getParametersRemote()
W
wuyi05 已提交
80

81 82
        :param pserver_spec: pserver location, eg: localhost:3000, if use etcd,
        pserver_spec should be the etcd endpoints, eg: http://localhost:2379
Q
qiaolongfei 已提交
83 84
        :return: parameter_updater
        """
Q
qiaolongfei 已提交
85
        if is_local:
Q
qiaolongfei 已提交
86
            parameter_updater = self.__create_local_updater__()
Q
qiaolongfei 已提交
87
        else:
88 89 90 91 92
            if pserver_spec is None:
                parameter_updater = self.__create_remote_updater__(
                    num_passes, use_sparse_updater)
            else:
                parameter_updater = self.__create_new_remote_updater__(
93
                    pserver_spec, use_etcd)
Q
qiaolongfei 已提交
94
        return parameter_updater
Q
qiaolongfei 已提交
95 96


L
Luo Tao 已提交
97
class Momentum(Optimizer):
Q
qijun 已提交
98
    """
99
    Momentum Optimizer.
Q
qijun 已提交
100

101
    When sparse=False, the momentum update formula is as follows:
Q
qijun 已提交
102 103 104

    ..  math::

D
dangqingqing 已提交
105
        v_{t} &= k * v_{t-1} - \\gamma_t (g_{t} + \\lambda w_{t-1}) \\\\
106
        w_{t} &= w_{t-1} + v_{t} \\\\
Q
qijun 已提交
107

108 109 110 111
    where, :math:`k` is momentum, :math:`\\lambda` is decay rate,
    :math:`\\gamma_t` is learning rate at the t'th iteration.
    :math:`w_{t}` is the weight as the t'th iteration.
    And the :math:`v_{t}` is the history momentum variable.
Q
qijun 已提交
112

113
    When sparse=True, the update scheme:
Q
qijun 已提交
114 115 116

    ..  math::

117 118 119 120 121 122 123 124 125 126 127 128 129
        \\alpha_t &= \\alpha_{t-1} / k \\\\
        \\beta_t &= \\beta_{t-1} / (1 + \\lambda \\gamma_t) \\\\
        u_t &= u_{t-1} - \\alpha_t \\gamma_t g_t \\\\
        v_t &= v_{t-1} + \\tau_{t-1} \\alpha_t \\gamma_t g_t \\\\
        \\tau_t &= \\tau_{t-1} + \\beta_t / \\alpha_t
    
    where :math:`k` is momentum, :math:`\\lambda` is decay rate, 
    :math:`\\gamma_t` is learning rate at the t'th iteration.

    :param momentum: the momentum factor.
    :type momentum: float
    :param sparse: with sparse support or not, False by default.
    :type sparse: bool
Q
qijun 已提交
130 131
    """

L
Luo Tao 已提交
132 133
    def __init__(self, momentum=None, sparse=False, **kwargs):
        learning_method = v1_optimizers.MomentumOptimizer(
Y
Yu Yang 已提交
134
            momentum=momentum, sparse=sparse)
L
Luo Tao 已提交
135 136 137 138
        super(Momentum, self).__init__(
            learning_method=learning_method, **kwargs)


Q
qiaolongfei 已提交
139
class Adam(Optimizer):
Q
qijun 已提交
140 141 142 143 144 145 146 147 148
    """
    Adam optimizer.
    The details of please refer `Adam: A Method for Stochastic Optimization
    <https://arxiv.org/abs/1412.6980>`_

    ..  math::

        m(w, t) & = \\beta_1 m(w, t-1) + (1 - \\beta_1) \\nabla Q_i(w) \\\\
        v(w, t) & = \\beta_2 v(w, t-1) + (1 - \\beta_2)(\\nabla Q_i(w)) ^2 \\\\
149
        w & = w - \\frac{\\eta m(w, t)}{\\sqrt{v(w,t) + \\epsilon}}
Q
qijun 已提交
150 151 152 153 154 155 156 157 158 159

    :param beta1: the :math:`\\beta_1` in equation.
    :type beta1: float
    :param beta2: the :math:`\\beta_2` in equation.
    :type beta2: float
    :param epsilon: the :math:`\\epsilon` in equation. It is used to prevent
                        divided by zero.
    :type epsilon: float
    """

Q
qiaolongfei 已提交
160 161 162 163 164 165 166
    def __init__(self, beta1=0.9, beta2=0.999, epsilon=1e-8, **kwargs):
        learning_method = v1_optimizers.AdamOptimizer(
            beta1=beta1, beta2=beta2, epsilon=epsilon)
        super(Adam, self).__init__(learning_method=learning_method, **kwargs)


class Adamax(Optimizer):
Q
qijun 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    """
    Adamax optimizer.

    The details of please refer this `Adam: A Method for Stochastic Optimization
    <https://arxiv.org/abs/1412.6980>`_

    ..  math::

        m_t & = \\beta_1 * m_{t-1} + (1-\\beta_1)* \\nabla Q_i(w) \\\\
        u_t & = max(\\beta_2*u_{t-1}, abs(\\nabla Q_i(w))) \\\\
        w_t & = w_{t-1} - (\\eta/(1-\\beta_1^t))*m_t/u_t

    :param beta1: the :math:`\\beta_1` in the equation.
    :type beta1: float
    :param beta2: the :math:`\\beta_2` in the equation.
    :type beta2: float
    """

Q
qiaolongfei 已提交
185 186 187 188 189 190
    def __init__(self, beta1=0.9, beta2=0.999, **kwargs):
        learning_method = v1_optimizers.AdamaxOptimizer(
            beta1=beta1, beta2=beta2)
        super(Adamax, self).__init__(learning_method=learning_method, **kwargs)


L
Luo Tao 已提交
191
class AdaGrad(Optimizer):
Q
qijun 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204
    """
    Adagrad(for ADAptive GRAdient algorithm) optimizer.

    For details please refer this `Adaptive Subgradient Methods for
    Online Learning and Stochastic Optimization
    <http://www.magicbroom.info/Papers/DuchiHaSi10.pdf>`_.

    ..  math::

        G &= \\sum_{\\tau=1}^{t} g_{\\tau} g_{\\tau}^T \\\\
        w & = w - \\eta diag(G)^{-\\frac{1}{2}} \\circ g
    """

L
Luo Tao 已提交
205 206 207 208 209 210
    def __init__(self, **kwargs):
        learning_method = v1_optimizers.AdaGradOptimizer()
        super(AdaGrad, self).__init__(learning_method=learning_method, **kwargs)


class DecayedAdaGrad(Optimizer):
Q
qijun 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    """
    AdaGrad method with decayed sum gradients. The equations of this method
    show as follow.

    ..  math::

        E(g_t^2) &= \\rho * E(g_{t-1}^2) + (1-\\rho) * g^2 \\\\
        learning\\_rate &= 1/sqrt( ( E(g_t^2) + \\epsilon )

    :param rho: The :math:`\\rho` parameter in that equation
    :type rho: float
    :param epsilon: The :math:`\\epsilon` parameter in that equation.
    :type epsilon: float
    """

L
Luo Tao 已提交
226 227 228 229 230 231 232 233
    def __init__(self, rho=0.95, epsilon=1e-06, **kwargs):
        learning_method = v1_optimizers.DecayedAdaGradOptimizer(
            rho=rho, epsilon=epsilon)
        super(DecayedAdaGrad, self).__init__(
            learning_method=learning_method, **kwargs)


class AdaDelta(Optimizer):
Q
qijun 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    """
    AdaDelta method. The details of adadelta please refer to this
    `ADADELTA: AN ADAPTIVE LEARNING RATE METHOD
    <http://www.matthewzeiler.com/pubs/googleTR2012/googleTR2012.pdf>`_.

    ..  math::

        E(g_t^2) &= \\rho * E(g_{t-1}^2) + (1-\\rho) * g^2 \\\\
        learning\\_rate &= sqrt( ( E(dx_{t-1}^2) + \\epsilon ) / ( \\
                          E(g_t^2) + \\epsilon ) ) \\\\
        E(dx_t^2) &= \\rho * E(dx_{t-1}^2) + (1-\\rho) * (-g*learning\\_rate)^2

    :param rho: :math:`\\rho` in equation
    :type rho: float
    :param epsilon: :math:`\\rho` in equation
    :type epsilon: float
    """
Q
qijun 已提交
251

L
Luo Tao 已提交
252 253 254 255 256 257 258 259
    def __init__(self, rho=0.95, epsilon=1e-06, **kwargs):
        learning_method = v1_optimizers.AdaDeltaOptimizer(
            rho=rho, epsilon=epsilon)
        super(AdaDelta, self).__init__(
            learning_method=learning_method, **kwargs)


class RMSProp(Optimizer):
Q
qijun 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    """
    RMSProp(for Root Mean Square Propagation) optimizer. For details please
    refer this `slide <http://www.cs.toronto.edu/~tijmen/csc321/slides/
    lecture_slides_lec6.pdf>`_.

    The equations of this method as follows:

    ..  math::

        v(w, t) & = \\rho v(w, t-1) + (1 - \\rho)(\\nabla Q_{i}(w))^2 \\\\
        w & = w - \\frac{\\eta} {\\sqrt{v(w,t) + \\epsilon}} \\nabla Q_{i}(w)

    :param rho: the :math:`\\rho` in the equation. The forgetting factor.
    :type rho: float
    :param epsilon: the :math:`\\epsilon` in the equation.
    :type epsilon: float
    """

L
Luo Tao 已提交
278 279 280 281 282 283 284 285 286
    def __init__(self, rho=0.95, epsilon=1e-6, **kwargs):
        learning_method = v1_optimizers.RMSPropOptimizer(
            rho=rho, epsilon=epsilon)
        super(RMSProp, self).__init__(learning_method=learning_method, **kwargs)


ModelAverage = v1_optimizers.ModelAverage
L2Regularization = v1_optimizers.L2Regularization

Q
qiaolongfei 已提交
287
if __name__ == '__main__':
Y
Yu Yang 已提交
288
    import py_paddle.swig_paddle as swig_api
Q
qiaolongfei 已提交
289
    swig_api.initPaddle('--use_gpu=false')
L
Luo Tao 已提交
290 291 292 293 294 295 296 297
    for opt in [
            Momentum(), Adam(), Adamax(), AdaGrad(), DecayedAdaGrad(),
            AdaDelta(), RMSProp(), Adam(
                model_average=ModelAverage(average_window=0.5),
                regularization=L2Regularization(rate=0.5),
                gradient_clipping_threshold=25)
    ]:
        print opt, opt.enable_types()