layer_math.py 3.9 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
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())
L
Luo Tao 已提交
42
register_unary_math_op('relu', act.ReluActivation())
X
xuwei06 已提交
43 44
register_unary_math_op('sqrt', act.SqrtActivation())
register_unary_math_op('reciprocal', act.ReciprocalActivation())
45

Q
qijun 已提交
46

47 48 49
def add(layeroutput, other):
    if is_compatible_with(other, float):
        return slope_intercept_layer(input=layeroutput, intercept=other)
X
xuwei06 已提交
50 51 52 53
    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 已提交
54 55 56 57
        return mixed_layer(input=[
            identity_projection(input=layeroutput),
            identity_projection(input=other)
        ])
X
xuwei06 已提交
58 59 60 61 62 63 64 65 66
    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 已提交
67 68 69 70
    return mixed_layer(input=[
        identity_projection(input=layeroutput), identity_projection(input=other)
    ])

71 72 73 74

LayerOutput.__radd__ = add
LayerOutput.__add__ = add

Q
qijun 已提交
75

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

Q
qijun 已提交
85

86 87
LayerOutput.__sub__ = sub

Q
qijun 已提交
88

89 90 91 92
def rsub(layeroutput, other):
    neg = slope_intercept_layer(input=layeroutput, slope=-1.0)
    return add(neg, other)

Q
qijun 已提交
93

94
LayerOutput.__rsub__ = rsub
X
xuwei06 已提交
95

Q
qijun 已提交
96

X
xuwei06 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110
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 已提交
111

X
xuwei06 已提交
112 113
LayerOutput.__mul__ = mul
LayerOutput.__rmul__ = mul