activations.py 5.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
#
# 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
    "STanhActivation", "AbsActivation", "SquareActivation", "BaseActivation",
C
caoying03 已提交
20 21
    "LogActivation", "SqrtActivation", "ReciprocalActivation",
    "SoftSignActivation"
Q
qijun 已提交
22
]
Z
zhangjinchao01 已提交
23 24 25 26


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

L
luotao02 已提交
30 31 32 33 34 35
    :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 已提交
36 37 38 39 40 41
    """

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

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

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

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

    .. math::

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

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


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

    .. math::

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

Q
qijun 已提交
68 69
    def __init__(self):
        BaseActivation.__init__(self, 'sigmoid', True)
Z
zhangjinchao01 已提交
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 109


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 已提交
110 111
    def __init__(self):
        BaseActivation.__init__(self, '', False)
Z
zhangjinchao01 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130


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 已提交
131 132
    def __init__(self):
        BaseActivation.__init__(self, 'relu', True)
Z
zhangjinchao01 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148


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 已提交
149 150
    def __init__(self):
        BaseActivation.__init__(self, 'brelu', False)
Z
zhangjinchao01 已提交
151 152 153 154 155 156 157


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

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

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

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

    .. math::

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

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


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 已提交
190 191
    def __init__(self):
        BaseActivation.__init__(self, 'abs', False)
Z
zhangjinchao01 已提交
192 193 194 195 196 197 198 199 200 201


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

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

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

205 206 207 208

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

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

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

217 218 219 220 221 222 223 224

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

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

    def __init__(self):
        BaseActivation.__init__(self, 'log', False)
X
xuwei06 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246


class SqrtActivation(BaseActivation):
    """
    Square Root Activation.

    .. math::
       f(z) = sqrt(z)
    """

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


class ReciprocalActivation(BaseActivation):
    """
    Reciprocal Activation.

    .. math::
C
caoying03 已提交
247
       f(z)=\\frac{1}{z}
X
xuwei06 已提交
248 249 250 251
    """

    def __init__(self):
        BaseActivation.__init__(self, 'reciprocal', False)
C
caoying03 已提交
252 253 254 255 256 257 258


class SoftSignActivation(BaseActivation):
    """
    SoftSign Activation.

    .. math::
C
caoying03 已提交
259
       f(z)=\\frac{z}{1 + |z|}
C
caoying03 已提交
260 261 262 263
    """

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