activations.py 4.8 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2016 Baidu, Inc. 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
qijun 已提交
15 16 17 18
__all__ = [
    "TanhActivation", "SigmoidActivation", "SoftmaxActivation",
    "IdentityActivation", "LinearActivation", 'SequenceSoftmaxActivation',
    'ExpActivation', "ReluActivation", "BReluActivation", "SoftReluActivation",
L
Luo Tao 已提交
19 20
    "STanhActivation", "AbsActivation", "SquareActivation", "BaseActivation",
    "LogActivation"
Q
qijun 已提交
21
]
Z
zhangjinchao01 已提交
22 23 24 25


class BaseActivation(object):
    """
26
    A mark for activation class.
L
luotao02 已提交
27
    Each activation inherit BaseActivation, which has two parameters.
28

L
luotao02 已提交
29 30 31 32 33 34
    :param name: activation name in paddle config.
    :type name: basestring
    :param support_hppl: True if supported by hppl. HPPL is a library used by paddle
                         internally. Currently, lstm layer can only use activations
                         supported by hppl.
    :type support_hppl: bool
Z
zhangjinchao01 已提交
35 36 37 38 39 40
    """

    def __init__(self, name, support_hppl):
        self.name = name
        self.support_hppl = support_hppl

41 42 43
    def __repr__(self):
        return self.name

Z
zhangjinchao01 已提交
44 45 46 47 48 49 50 51 52 53

class TanhActivation(BaseActivation):
    """
    Tanh activation.

    .. math::

       f(z)=tanh(z)=\\frac{e^z-e^{-z}}{e^z+e^{-z}}
    """

Q
qijun 已提交
54 55
    def __init__(self):
        BaseActivation.__init__(self, 'tanh', True)
Z
zhangjinchao01 已提交
56 57 58 59 60 61 62 63 64 65 66


class SigmoidActivation(BaseActivation):
    """
    Sigmoid activation.

    .. math::

       f(z) = \\frac{1}{1+exp(-z)}
    """

Q
qijun 已提交
67 68
    def __init__(self):
        BaseActivation.__init__(self, 'sigmoid', True)
Z
zhangjinchao01 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108


class SoftmaxActivation(BaseActivation):
    """
    Softmax activation for simple input



    .. math::

       P(y=j|x) = \\frac{e^{x_j}} {\\sum^K_{k=1} e^{x_j} }
    """

    def __init__(self):
        BaseActivation.__init__(self, 'softmax', False)


class SequenceSoftmaxActivation(BaseActivation):
    """
    Softmax activation for one sequence. The dimension of input feature must be
    1 and a sequence.

    ..  code:: python

        result = softmax(for each_feature_vector[0] in input_feature)
        for i, each_time_step_output in enumerate(output):
            each_time_step_output = result[i]
    """

    def __init__(self):
        BaseActivation.__init__(self, 'sequence_softmax', False)


class IdentityActivation(BaseActivation):
    """
    Identity Activation.

    Just do nothing for output both forward/backward.
    """

Q
qijun 已提交
109 110
    def __init__(self):
        BaseActivation.__init__(self, '', False)
Z
zhangjinchao01 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129


LinearActivation = IdentityActivation


class ReluActivation(BaseActivation):
    """
    Relu activation.

    forward. :math:`y = max(0, z)`

    derivative:

    .. math::

       1  &\\quad if z > 0 \\\\
       0  &\\quad\\mathrm{otherwize}
    """

Q
qijun 已提交
130 131
    def __init__(self):
        BaseActivation.__init__(self, 'relu', True)
Z
zhangjinchao01 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147


class BReluActivation(BaseActivation):
    """
    BRelu Activation.

    forward.  :math:`y = min(24, max(0, z))`

    derivative:

    .. math::

       1  &\\quad if 0 < z < 24 \\\\
       0  &\\quad \\mathrm{otherwise}
    """

Q
qijun 已提交
148 149
    def __init__(self):
        BaseActivation.__init__(self, 'brelu', False)
Z
zhangjinchao01 已提交
150 151 152 153 154 155 156


class SoftReluActivation(BaseActivation):
    """
    SoftRelu Activation.
    """

Q
qijun 已提交
157 158 159
    def __init__(self):
        BaseActivation.__init__(self, 'softrelu', False)

Z
zhangjinchao01 已提交
160 161 162 163 164 165 166 167 168 169

class STanhActivation(BaseActivation):
    """
    Scaled Tanh Activation.

    .. math::

       f(z) = 1.7159 * tanh(2/3*z)
    """

Q
qijun 已提交
170 171
    def __init__(self):
        BaseActivation.__init__(self, 'stanh', False)
Z
zhangjinchao01 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188


class AbsActivation(BaseActivation):
    """
    Abs Activation.

    Forward:    :math:`f(z) = abs(z)`

    Derivative:

    .. math::

       1 &\\quad if \\quad z > 0 \\\\
       -1 &\\quad if \\quad z < 0 \\\\
       0 &\\quad if \\quad z = 0
    """

Q
qijun 已提交
189 190
    def __init__(self):
        BaseActivation.__init__(self, 'abs', False)
Z
zhangjinchao01 已提交
191 192 193 194 195 196 197 198 199 200


class SquareActivation(BaseActivation):
    """
    Square Activation.

    .. math::
       f(z) = z^2.
    """

Q
qijun 已提交
201 202 203
    def __init__(self):
        BaseActivation.__init__(self, 'square', False)

204 205 206 207

class ExpActivation(BaseActivation):
    """
    Exponential Activation.
208

209 210 211
    .. math::
       f(z) = e^z.
    """
Q
qijun 已提交
212 213 214 215

    def __init__(self):
        BaseActivation.__init__(self, 'exponential', False)

216 217 218 219 220 221 222 223

class LogActivation(BaseActivation):
    """
    Logarithm Activation.

    .. math::
       f(z) = log(z)
    """
Q
qijun 已提交
224 225 226

    def __init__(self):
        BaseActivation.__init__(self, 'log', False)