activations.py 4.7 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 19 20
__all__ = [
    "TanhActivation", "SigmoidActivation", "SoftmaxActivation",
    "IdentityActivation", "LinearActivation", 'SequenceSoftmaxActivation',
    'ExpActivation', "ReluActivation", "BReluActivation", "SoftReluActivation",
    "STanhActivation", "AbsActivation", "SquareActivation", "BaseActivation"
]
Z
zhangjinchao01 已提交
21 22 23 24


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

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

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

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

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

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

    .. math::

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

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


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

    .. math::

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

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


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


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


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


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

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

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

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

    .. math::

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

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


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


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

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

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

203 204 205 206

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

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

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

215 216 217 218 219 220 221 222

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

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

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