Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/mxnet/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def __mul__(self, other):
raise TypeError('type %s not supported' % str(type(other)))

def __neg__(self):
return NDArray._mul_scalar(self, -1.0, out=self)
return NDArray._mul_scalar(self, -1.0)

def __imul__(self, other):
if isinstance(other, NDArray):
Expand Down
15 changes: 14 additions & 1 deletion tests/python/unittest/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ def test_ndarray_elementwise():
check_with_uniform(lambda x, y: x * y, 2, dim)
check_with_uniform(lambda x, y: x / y, 2, dim)

def test_ndarray_negate():
npy = np.random.uniform(-10, 10, (2,3,4))
arr = mx.nd.array(npy)
assert reldiff(npy, arr.asnumpy()) < 1e-6
assert reldiff(-npy, (-arr).asnumpy()) < 1e-6

# a final check to make sure the negation (-) is not implemented
# as inplace operation, so the contents of arr does not change after
# we compute (-arr)
assert reldiff(npy, arr.asnumpy()) < 1e-6


def test_ndarray_copy():
c = mx.nd.array(np.random.uniform(-10, 10, (10, 10)))
d = c.copyto(mx.Context('cpu', 0))
Expand All @@ -67,7 +79,7 @@ def test_ndarray_scalar():
c[:] = 2
assert(np.sum(c.asnumpy()) - 200 < 1e-5)
d = -c + 2
assert(np.sum(c.asnumpy()) < 1e-5)
assert(np.sum(d.asnumpy()) < 1e-5)

def test_ndarray_pickle():
np.random.seed(0)
Expand Down Expand Up @@ -142,6 +154,7 @@ def test_dot():
test_ndarray_saveload()
test_ndarray_copy()
test_ndarray_elementwise()
test_ndarray_negate()
test_ndarray_scalar()
test_clip()
test_dot()