提交 b0c64aae authored 作者: Frederic Bastien's avatar Frederic Bastien

added scalar and elemwise tensor op maximum and mininum and test for them.

numpy define the fct maximum and mininum as doing elemwise max and min. We have the theano.tensor.{largest, smallest} that did that but with more op. That make the graph harder to read. We will probably map largest and smallest to the new fct in the futur after time show they are good.
上级 9d3a6787
......@@ -726,6 +726,32 @@ invert = Invert()
# Arithmetic
##############
class Maximum(BinaryScalarOp):
commutative = True
associative = True
def impl(self, *inputs):
return max(inputs)
def c_code(self, node, name, (x,y), (z, ), sub):
return "%(z)s = ((%(y)s)>(%(x)s)? (%(y)s):(%(x)s));" %locals()
def grad(self, (x, y), (gz, )):
return (eq(maximum(x,y), x)*gz,eq(maximum(x,y), y)*gz)
maximum = Maximum(upcast_out, name = 'maximum')
class Minimum(BinaryScalarOp):
commutative = True
associative = True
def impl(self, *inputs):
return min(inputs)
def c_code(self, node, name, (x,y), (z, ), sub):
return "%(z)s = ((%(y)s)<(%(x)s)? (%(y)s):(%(x)s));" %locals()
def grad(self, (x, y), (gz, )):
return (eq(minimum(x,y), x)*gz,eq(minimum(x,y), y)*gz)
minimum = Minimum(upcast_out, name = 'minimum')
class Add(ScalarOp):
identity = 0
commutative = True
......
......@@ -2029,6 +2029,17 @@ setdefault = default # legacy
##########################
# Arithmetics
##########################
@_scal_elemwise
def maximum(x,y):
"""elemwise maximum. See max for the maximum in one tensor
"""
# see decorator for function body
@_scal_elemwise
def minimum(x,y):
"""elemwise minimum. See min for the minimum in one tensor
"""
# see decorator for function body
def div_proxy(x, y):
"""Proxy for either true_div or int_div, depending on types of x, y.
......
......@@ -168,6 +168,14 @@ fill_inplace = second_inplace
pprint.assign(fill_inplace, printing.FunctionPrinter('fill='))
@_scal_inplace
def maximum_inplace(a, b):
"""elementwise addition (inplace on `a`)"""
@_scal_inplace
def minimum_inplace(a, b):
"""elementwise addition (inplace on `a`)"""
@_scal_inplace
def add_inplace(a, b):
"""elementwise addition (inplace on `a`)"""
......
......@@ -250,6 +250,36 @@ SubInplaceTester = makeBroadcastTester(op = inplace.sub_inplace,
grad = _grad_broadcast_binary_normal,
inplace = True)
MaximumTester = makeBroadcastTester(op = maximum,
expected = numpy.maximum,
good = _good_broadcast_binary_normal,
bad_build = _bad_build_broadcast_binary_normal,
bad_runtime = _bad_runtime_broadcast_binary_normal,
grad = _grad_broadcast_binary_normal)
MaximumInplaceTester = makeBroadcastTester(op = inplace.maximum_inplace,
expected = numpy.maximum,
good = _good_broadcast_binary_normal,
bad_build = _bad_build_broadcast_binary_normal,
bad_runtime = _bad_runtime_broadcast_binary_normal,
grad = _grad_broadcast_binary_normal,
inplace = True)
MinimumTester = makeBroadcastTester(op = minimum,
expected = numpy.minimum,
good = _good_broadcast_binary_normal,
bad_build = _bad_build_broadcast_binary_normal,
bad_runtime = _bad_runtime_broadcast_binary_normal,
grad = _grad_broadcast_binary_normal)
MinimumInplaceTester = makeBroadcastTester(op = inplace.minimum_inplace,
expected = numpy.minimum,
good = _good_broadcast_binary_normal,
bad_build = _bad_build_broadcast_binary_normal,
bad_runtime = _bad_runtime_broadcast_binary_normal,
grad = _grad_broadcast_binary_normal,
inplace = True)
MulTester = makeBroadcastTester(op = mul,
expected = lambda *inputs: reduce(lambda x, y: x * y, inputs),
good = dict(three_inputs_same_shapes = (rand(2, 3), rand(2, 3), rand(2, 3)),
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论