提交 08d9e9b1 authored 作者: Frederic Bastien's avatar Frederic Bastien

added tensor.round and tensor.iround and their doc.

上级 5f4093e7
......@@ -954,9 +954,13 @@ Mathematical
Returns a variable representing the floor of a (for example floor(2.9) is 2).
.. function:: iround(a)
.. function:: round(a, mode="half_away_from_zero")
Returns a variable representing the of rounding of a, ie int(a).
Returns a variable representing the rounding of a in the same dtype as a. Implemented rounding mode are half_away_from_zero and half_to_even.
.. function:: iround(a, mode="half_away_from_zero")
Short hand for cast(round(a, mode),'int64').
.. function:: sqr(a)
......
......@@ -1145,13 +1145,6 @@ class Floor(UnaryScalarOp):
return "%(z)s = floor(%(x)s);" % locals()
floor = Floor(same_out_nocomplex, name = 'floor')
class IRound(UnaryScalarOp):
def impl(self, x):
return theano._asarray(numpy.round(x), dtype = 'int64')
def c_code(self, node, name, (x, ), (z, ), sub):
return "%(z)s = round(%(x)s);" % locals()
iround = IRound(int_out_nocomplex)
class RoundHalfToEven(UnaryScalarOp):
"""
This function implement the same rounding than numpy: Round half to even
......
......@@ -1574,9 +1574,20 @@ def ceil(a):
def floor(a):
"""floor of a"""
@_scal_elemwise
def iround(a):
"""int(round(a))"""
@constructor
def iround(a, mode="half_away_from_zero"):
"""cast(round(a,mode),'int64')"""
return cast(round(a,mode),'int64')
@constructor
def round(a, mode="half_away_from_zero"):
"""round_mode(a) with mode in [half_away_from_zero, half_to_even]"""
if mode == "half_away_from_zero":
return round_half_away_from_zero(a)
elif mode == "half_to_even":
return round_half_to_even(a)
else:
raise Exception("round mode %s is not implemented."%mode)
@_scal_elemwise
def round_half_to_even(a):
......
......@@ -124,10 +124,6 @@ def ceil_inplace(a):
def floor_inplace(a):
"""floor of `a` (inplace on `a`)"""
@_scal_inplace
def iround_inplace(a):
"""int(round(a)) (inplace on `a`)"""
@_scal_inplace
def round_half_to_even_inplace(a):
"""round_half_to_even_inplace(a) (inplace on `a`)"""
......
......@@ -445,14 +445,6 @@ FloorInplaceTester = makeBroadcastTester(op = inplace.floor_inplace,
grad = _grad_broadcast_unary_normal,
inplace = True)
IRoundTester = makeBroadcastTester(op = iround,
expected = lambda a: numpy.asarray(numpy.round(a),dtype='int64'),
good = _good_broadcast_unary_normal)
IRoundInplaceTester = makeBroadcastTester(op = inplace.iround_inplace,
expected = lambda a: numpy.asarray(numpy.round(a),dtype='int64'),
good = _good_broadcast_unary_normal,
inplace = True)
RoundHalfToEvenTester = makeBroadcastTester(op = round_half_to_even,
expected = numpy.round,
good = _good_broadcast_unary_normal_float)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论