math.py 3.8 KB
Newer Older
1
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#
# 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.

from .layers import LayerOutput, mixed_layer, identity_projection, \
X
xuwei06 已提交
16
    slope_intercept_layer, scaling_layer, repeat_layer
17 18 19
from .attrs import is_compatible_with
from .default_decorators import *
import activations as act
X
xuwei06 已提交
20
from paddle.trainer.config_parser import logger
21 22 23

__all__ = []

Q
qijun 已提交
24

25 26
def register_unary_math_op(op_name, act):
    def op(input, name=None):
Q
qijun 已提交
27 28 29
        return mixed_layer(
            input=[identity_projection(input=input)], name=name, act=act)

30 31 32 33 34
    op = wrap_name_default(op_name)(op)
    op.__doc__ = type(act).__doc__
    globals()[op_name] = op
    __all__.append(op_name)

Q
qijun 已提交
35

36 37 38 39 40 41 42
register_unary_math_op('exp', act.ExpActivation())
register_unary_math_op('log', act.LogActivation())
register_unary_math_op('abs', act.AbsActivation())
register_unary_math_op('sigmoid', act.SigmoidActivation())
register_unary_math_op('tanh', act.TanhActivation())
register_unary_math_op('square', act.SquareActivation())

Q
qijun 已提交
43

44 45 46
def add(layeroutput, other):
    if is_compatible_with(other, float):
        return slope_intercept_layer(input=layeroutput, intercept=other)
X
xuwei06 已提交
47 48 49 50
    if not isinstance(other, LayerOutput):
        logger.fatal("LayerOutput can only be added with"
                     " another LayerOutput or a number")
    if layeroutput.size == other.size:
Q
qijun 已提交
51 52 53 54
        return mixed_layer(input=[
            identity_projection(input=layeroutput),
            identity_projection(input=other)
        ])
X
xuwei06 已提交
55 56 57 58 59 60 61 62 63
    if other.size != 1 and layeroutput.size != 1:
        logger.fatal("Two LayerOutput can be added only if they have equal size"
                     " or one of their sizes is 1. sizes are %s and %s" %
                     (layeroutput.size, other.size))
    elif layeroutput.size == 1:
        tmp = layeroutput
        layeroutput = other
        other = tmp
    other = repeat_layer(other, layeroutput.size)
Q
qijun 已提交
64 65 66 67
    return mixed_layer(input=[
        identity_projection(input=layeroutput), identity_projection(input=other)
    ])

68 69 70 71

LayerOutput.__radd__ = add
LayerOutput.__add__ = add

Q
qijun 已提交
72

73 74 75
def sub(layeroutput, other):
    if is_compatible_with(other, float):
        return slope_intercept_layer(input=layeroutput, intercept=other)
X
xuwei06 已提交
76 77 78
    if not isinstance(other, LayerOutput):
        logger.fatal("LayerOutput can only be subtracted with"
                     " another Layeroutput or a number")
79
    neg = slope_intercept_layer(input=other, slope=-1.0)
X
xuwei06 已提交
80
    return add(layeroutput, neg)
81

Q
qijun 已提交
82

83 84
LayerOutput.__sub__ = sub

Q
qijun 已提交
85

86 87 88 89
def rsub(layeroutput, other):
    neg = slope_intercept_layer(input=layeroutput, slope=-1.0)
    return add(neg, other)

Q
qijun 已提交
90

91
LayerOutput.__rsub__ = rsub
X
xuwei06 已提交
92

Q
qijun 已提交
93

X
xuwei06 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107
def mul(layeroutput, other):
    if is_compatible_with(other, float):
        return slope_intercept_layer(input=layeroutput, slope=other)
    if not isinstance(other, LayerOutput):
        logger.fatal("LayerOutput can only be multiplied with"
                     " another Layeroutput or a number")
    elif layeroutput.size == 1:
        return scaling_layer(input=other, weight=layeroutput)
    elif other.size == 1:
        return scaling_layer(input=layeroutput, weight=other)
    else:
        logger.fatal("At least one of the operand of '*' must be a number"
                     " or a LayerOutput with size=1")

Q
qijun 已提交
108

X
xuwei06 已提交
109 110
LayerOutput.__mul__ = mul
LayerOutput.__rmul__ = mul