Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test #91

Merged
merged 5 commits into from
Jun 14, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 16 additions & 16 deletions python/mxnet/sparse_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,44 +84,44 @@ class SparseNDArray(NDArray):
fixed-size items, stored in sparse format. See CSRNDArray and RowSparseNDArray
for more details.
"""
def __add__(self, other):
raise Exception('Not implemented for SparseND yet!')
# def __add__(self, other):
# raise Exception('Not implemented for SparseND yet!')

def __iadd__(self, other):
raise Exception('Not implemented for SparseND yet!')

def __radd__(self, other):
raise Exception('Not implemented for SparseND yet!')
# def __radd__(self, other):
# raise Exception('Not implemented for SparseND yet!')

def __isub__(self, other):
raise Exception('Not implemented for SparseND yet!')

def __rsub__(self, other):
raise Exception('Not implemented for SparseND yet!')
# def __rsub__(self, other):
# raise Exception('Not implemented for SparseND yet!')

def __imul__(self, other):
raise Exception('Not implemented for SparseND yet!')

def __rmul__(self, other):
raise Exception('Not implemented for SparseND yet!')
# def __rmul__(self, other):
# raise Exception('Not implemented for SparseND yet!')

def __rdiv__(self, other):
raise Exception('Not implemented for SparseND yet!')
# def __rdiv__(self, other):
# raise Exception('Not implemented for SparseND yet!')

def __idiv__(self, other):
raise Exception('Not implemented for SparseND yet!')

def __rtruediv__(self, other):
raise Exception('Not implemented for SparseND yet!')
# def __rtruediv__(self, other):
# raise Exception('Not implemented for SparseND yet!')

def __itruediv__(self, other):
raise Exception('Not implemented for SparseND yet!')

def __pow__(self, other):
raise Exception('Not implemented for SparseND yet!')
# def __pow__(self, other):
# raise Exception('Not implemented for SparseND yet!')

def __rpow__(self, other):
raise Exception('Not implemented for SparseND yet!')
# def __rpow__(self, other):
# raise Exception('Not implemented for SparseND yet!')

def __setitem__(self, key, value):
"""x.__setitem__(i, y) <=> x[i]=y
Expand Down
34 changes: 31 additions & 3 deletions tests/python/unittest/test_sparse_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,25 +257,53 @@ def check_binary(fn):
lshape[ndim-i-1] = 1
elif sep < 0.66:
rshape[bdim-i-1] = 1
lhs = np.random.normal(0, 1, size=lshape)
rhs = np.random.normal(0, 1, size=rshape)
lhs = np.random.uniform(0, 1, size=lshape)
rhs = np.random.uniform(0, 1, size=rshape)
lhs_nd = mx.nd.array(lhs).to_csr()
rhs_nd = mx.nd.array(rhs).to_csr()
assert_allclose(fn(lhs, rhs),
fn(lhs_nd, rhs_nd).asnumpy(),
rtol=1e-4, atol=1e-4)

#check_binary(lambda x, y: x + y)
check_binary(lambda x, y: x + y)
check_binary(lambda x, y: x - y)
check_binary(lambda x, y: x * y)
check_binary(lambda x, y: x / y)
check_binary(lambda x, y: x ** y)
check_binary(lambda x, y: x > y)
check_binary(lambda x, y: x < y)
check_binary(lambda x, y: x >= y)
check_binary(lambda x, y: x <= y)
check_binary(lambda x, y: x == y)


def test_sparse_nd_rhs_op_overload():
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about test_sparse_nd_binary_rop()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

N = 100
def check(fn):
for _ in range(N):
ndim = 2
shape = np.random.randint(1, 6, size=(ndim,))
np_nd = np.random.normal(0, 1, size=shape)
csr_nd = mx.nd.array(np_nd).to_csr()
assert_allclose(
fn(np_nd),
fn(csr_nd).asnumpy(),
rtol=1e-4,
atol=1e-4
)
check(lambda x: 1 + x)
check(lambda x: 1 - x)
check(lambda x: 1 * x)
check(lambda x: 1 / x)
check(lambda x: 2 ** x)
check(lambda x: 1 > x)
check(lambda x: 0.5 > x)
check(lambda x: 0.5 < x)
check(lambda x: 0.5 >= x)
check(lambda x: 0.5 <= x)
check(lambda x: 0.5 == x)


def test_sparse_nd_negate():
npy = np.random.uniform(-10, 10, rand_shape_2d())
arr = mx.nd.array(npy).to_csr()
Expand Down