Skip to content

Commit

Permalink
[Numpy] [Operator] Fix __neg__ (apache#18467)
Browse files Browse the repository at this point in the history
* fix negation

* support boolean type

* Update elemwise_unary_op.h

* Update test_numpy_op.py
  • Loading branch information
sxjscience authored and ys2843 committed Jun 2, 2020
1 parent 8cee26c commit f498378
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion python/mxnet/numpy/multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ def __mul__(self, other):
return multiply(self, other)

def __neg__(self):
return self.__mul__(-1.0)
return negative(self)

@wrap_mxnp_np_ufunc
def __imul__(self, other):
Expand Down
2 changes: 1 addition & 1 deletion python/mxnet/symbol/numpy/_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def __rpow__(self, other):

def __neg__(self):
"""x.__neg__() <=> - x"""
return self.__mul__(-1.0)
return negative(self)

def __deepcopy__(self, _):
return super(_Symbol, self).as_np_ndarray()
Expand Down
21 changes: 19 additions & 2 deletions tests/python/unittest/test_numpy_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -2373,10 +2373,10 @@ def hybrid_forward(self, F, a, *args, **kwargs):
return getattr(F.np, self._func)(a)

np_func = getattr(_np, func)
mx_func = TestUnary(func)
np_test_data = _np.random.uniform(low, high, shape).astype(_np.float32)
mx_test_data = mx.numpy.array(np_test_data)
for hybridize in [True, False]:
mx_func = TestUnary(func)
if hybridize:
mx_func.hybridize()
if ref_grad:
Expand Down Expand Up @@ -2420,13 +2420,30 @@ def hybrid_forward(self, F, a, *args, **kwargs):
funcs['rad2deg'] = (lambda x: 180. / _np.pi * _np.ones(x.shape), -1.0, 1.0)
funcs['deg2rad'] = (lambda x: _np.pi / 180. * _np.ones(x.shape), -1.0, 1.0)
ndim = random.choice([2, 3, 4])
shape = random.choice([rand_shape_nd(ndim, dim=3), (1, 0, 2)])
for shape in [rand_shape_nd(ndim, dim=3), (1, 0, 2)]:
for func, func_data in funcs.items():
ref_grad, low, high = func_data
check_unary_func(func, ref_grad, shape, low, high)


@use_np
def test_negation():
class TestNegation(HybridBlock):
def hybrid_forward(self, F, a):
return -a
mx_func = TestNegation()
for dtype in [_np.int8, _np.int32, _np.float16, _np.float32, _np.float64]:
np_test_data = _np.random.uniform(-1, 1, (5, 5)).astype(dtype)
for hybridize in [True, False]:
mx_test_data = mx.numpy.array(np_test_data, dtype=dtype)
if hybridize:
mx_func.hybridize()
y = mx_func(mx_test_data)
assert y.shape == (5, 5)
assert y.dtype == dtype
assert_almost_equal(y.asnumpy(), -np_test_data)


@with_seed()
@use_np
@retry(3)
Expand Down

0 comments on commit f498378

Please sign in to comment.