def add(layeroutput, other): if is_compatible_with(other, float): return slope_intercept_layer(input=layeroutput, intercept=other) 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) ])
def sub(layeroutput, other): if is_compatible_with(other, float): return slope_intercept_layer(input=layeroutput, intercept=other) 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 add(layeroutput, neg)
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")
def add(layeroutput, other): if is_compatible_with(other, float): return slope_intercept_layer(input=layeroutput, intercept=other) 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) ])