|
|
|
@ -13,10 +13,11 @@
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
|
|
from .layers import LayerOutput, mixed_layer, identity_projection, \
|
|
|
|
|
slope_intercept_layer
|
|
|
|
|
slope_intercept_layer, scaling_layer, repeat_layer
|
|
|
|
|
from .attrs import is_compatible_with
|
|
|
|
|
from .default_decorators import *
|
|
|
|
|
import activations as act
|
|
|
|
|
from paddle.trainer.config_parser import logger
|
|
|
|
|
|
|
|
|
|
__all__ = []
|
|
|
|
|
|
|
|
|
@ -40,7 +41,21 @@ register_unary_math_op('square', act.SquareActivation())
|
|
|
|
|
def add(layeroutput, other):
|
|
|
|
|
if is_compatible_with(other, float):
|
|
|
|
|
return slope_intercept_layer(input=layeroutput, intercept=other)
|
|
|
|
|
assert isinstance(other, LayerOutput)
|
|
|
|
|
if not isinstance(other, LayerOutput):
|
|
|
|
|
logger.fatal("LayerOutput can only be added with"
|
|
|
|
|
" another LayerOutput or a number")
|
|
|
|
|
if layeroutput.size == other.size:
|
|
|
|
|
return mixed_layer(input=[identity_projection(input=layeroutput),
|
|
|
|
|
identity_projection(input=other)])
|
|
|
|
|
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)
|
|
|
|
|
return mixed_layer(input=[identity_projection(input=layeroutput),
|
|
|
|
|
identity_projection(input=other)])
|
|
|
|
|
|
|
|
|
@ -50,10 +65,11 @@ LayerOutput.__add__ = add
|
|
|
|
|
def sub(layeroutput, other):
|
|
|
|
|
if is_compatible_with(other, float):
|
|
|
|
|
return slope_intercept_layer(input=layeroutput, intercept=other)
|
|
|
|
|
assert isinstance(other, LayerOutput)
|
|
|
|
|
if not isinstance(other, LayerOutput):
|
|
|
|
|
logger.fatal("LayerOutput can only be subtracted with"
|
|
|
|
|
" another Layeroutput or a number")
|
|
|
|
|
neg = slope_intercept_layer(input=other, slope=-1.0)
|
|
|
|
|
return mixed_layer(input=[identity_projection(input=layeroutput),
|
|
|
|
|
identity_projection(input=neg)])
|
|
|
|
|
return add(layeroutput, neg)
|
|
|
|
|
|
|
|
|
|
LayerOutput.__sub__ = sub
|
|
|
|
|
|
|
|
|
@ -62,3 +78,20 @@ def rsub(layeroutput, other):
|
|
|
|
|
return add(neg, other)
|
|
|
|
|
|
|
|
|
|
LayerOutput.__rsub__ = rsub
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
LayerOutput.__mul__ = mul
|
|
|
|
|
LayerOutput.__rmul__ = mul
|
|
|
|
|