Skip to content

Commit

Permalink
Add unit test (apache#91)
Browse files Browse the repository at this point in the history
* add unittest

* minor fix

* remove commented lines

* change test func name

* add test rsp
  • Loading branch information
jiajiechen authored and Olivier committed Jun 15, 2017
1 parent 518306f commit 9d076b5
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 101 deletions.
24 changes: 0 additions & 24 deletions python/mxnet/sparse_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,45 +84,21 @@ 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 __iadd__(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 __imul__(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 __idiv__(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 __rpow__(self, other):
raise Exception('Not implemented for SparseND yet!')

def __setitem__(self, key, value):
"""x.__setitem__(i, y) <=> x[i]=y
Expand Down
189 changes: 112 additions & 77 deletions tests/python/unittest/test_sparse_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,81 +165,81 @@ def check_sparse_nd_csr_slice(shape):


def test_sparse_nd_equal():
stype = 'csr'
shape = rand_shape_2d()
x = mx.sparse_nd.zeros(stype, shape)
y = sparse_nd_ones(shape, stype)
z = x == y
assert (z.asnumpy() == np.zeros(shape)).all()
z = 0 == x
assert (z.asnumpy() == np.ones(shape)).all()
for stype in ['row_sparse', 'csr']:
shape = rand_shape_2d()
x = mx.sparse_nd.zeros(stype, shape)
y = sparse_nd_ones(shape, stype)
z = x == y
assert (z.asnumpy() == np.zeros(shape)).all()
z = 0 == x
assert (z.asnumpy() == np.ones(shape)).all()


def test_sparse_nd_not_equal():
stype = 'csr'
shape = rand_shape_2d()
x = mx.sparse_nd.zeros(stype, shape)
y = sparse_nd_ones(shape, stype)
z = x != y
assert (z.asnumpy() == np.ones(shape)).all()
z = 0 != x
assert (z.asnumpy() == np.zeros(shape)).all()
for stype in ['row_sparse', 'csr']:
shape = rand_shape_2d()
x = mx.sparse_nd.zeros(stype, shape)
y = sparse_nd_ones(shape, stype)
z = x != y
assert (z.asnumpy() == np.ones(shape)).all()
z = 0 != x
assert (z.asnumpy() == np.zeros(shape)).all()


def test_sparse_nd_greater():
stype = 'csr'
shape = rand_shape_2d()
x = mx.sparse_nd.zeros(stype, shape)
y = sparse_nd_ones(shape, stype)
z = x > y
assert (z.asnumpy() == np.zeros(shape)).all()
z = y > 0
assert (z.asnumpy() == np.ones(shape)).all()
z = 0 > y
assert (z.asnumpy() == np.zeros(shape)).all()
for stype in ['row_sparse', 'csr']:
shape = rand_shape_2d()
x = mx.sparse_nd.zeros(stype, shape)
y = sparse_nd_ones(shape, stype)
z = x > y
assert (z.asnumpy() == np.zeros(shape)).all()
z = y > 0
assert (z.asnumpy() == np.ones(shape)).all()
z = 0 > y
assert (z.asnumpy() == np.zeros(shape)).all()


def test_sparse_nd_greater_equal():
stype = 'csr'
shape = rand_shape_2d()
x = mx.sparse_nd.zeros(stype, shape)
y = sparse_nd_ones(shape, stype)
z = x >= y
assert (z.asnumpy() == np.zeros(shape)).all()
z = y >= 0
assert (z.asnumpy() == np.ones(shape)).all()
z = 0 >= y
assert (z.asnumpy() == np.zeros(shape)).all()
z = y >= 1
assert (z.asnumpy() == np.ones(shape)).all()
for stype in ['row_sparse', 'csr']:
shape = rand_shape_2d()
x = mx.sparse_nd.zeros(stype, shape)
y = sparse_nd_ones(shape, stype)
z = x >= y
assert (z.asnumpy() == np.zeros(shape)).all()
z = y >= 0
assert (z.asnumpy() == np.ones(shape)).all()
z = 0 >= y
assert (z.asnumpy() == np.zeros(shape)).all()
z = y >= 1
assert (z.asnumpy() == np.ones(shape)).all()


def test_sparse_nd_lesser():
stype = 'csr'
shape = rand_shape_2d()
x = mx.sparse_nd.zeros(stype, shape)
y = sparse_nd_ones(shape, stype)
z = y < x
assert (z.asnumpy() == np.zeros(shape)).all()
z = 0 < y
assert (z.asnumpy() == np.ones(shape)).all()
z = y < 0
assert (z.asnumpy() == np.zeros(shape)).all()
for stype in ['row_sparse', 'csr']:
shape = rand_shape_2d()
x = mx.sparse_nd.zeros(stype, shape)
y = sparse_nd_ones(shape, stype)
z = y < x
assert (z.asnumpy() == np.zeros(shape)).all()
z = 0 < y
assert (z.asnumpy() == np.ones(shape)).all()
z = y < 0
assert (z.asnumpy() == np.zeros(shape)).all()


def test_sparse_nd_lesser_equal():
stype = 'csr'
shape = rand_shape_2d()
x = mx.sparse_nd.zeros(stype, shape)
y = sparse_nd_ones(shape, stype)
z = y <= x
assert (z.asnumpy() == np.zeros(shape)).all()
z = 0 <= y
assert (z.asnumpy() == np.ones(shape)).all()
z = y <= 0
assert (z.asnumpy() == np.zeros(shape)).all()
z = 1 <= y
assert (z.asnumpy() == np.ones(shape)).all()
for stype in ['row_sparse', 'csr']:
shape = rand_shape_2d()
x = mx.sparse_nd.zeros(stype, shape)
y = sparse_nd_ones(shape, stype)
z = y <= x
assert (z.asnumpy() == np.zeros(shape)).all()
z = 0 <= y
assert (z.asnumpy() == np.ones(shape)).all()
z = y <= 0
assert (z.asnumpy() == np.zeros(shape)).all()
z = 1 <= y
assert (z.asnumpy() == np.ones(shape)).all()


def test_sparse_nd_binary():
Expand All @@ -257,35 +257,70 @@ 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_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)
lhs = np.random.uniform(0, 1, size=lshape)
rhs = np.random.uniform(0, 1, size=rshape)
lhs_nd_csr = mx.nd.array(lhs).to_csr()
rhs_nd_csr = mx.nd.array(rhs).to_csr()
lhs_nd_rsp = mx.nd.array(lhs).to_rsp()
rhs_nd_rsp = mx.nd.array(rhs).to_rsp()
for lhs_nd, rhs_nd in [(lhs_nd_csr, rhs_nd_csr), (lhs_nd_rsp, rhs_nd_rsp)]:
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)


def test_sparse_nd_binary_rop():
N = 100
def check(fn):
for _ in range(N):
ndim = 2
shape = np.random.randint(1, 6, size=(ndim,))
npy_nd = np.random.normal(0, 1, size=shape)
csr_nd = mx.nd.array(npy_nd).to_csr()
rsp_nd = mx.nd.array(npy_nd).to_rsp()
for sparse_nd in [csr_nd, rsp_nd]:
assert_allclose(
fn(npy_nd),
fn(sparse_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()
assert_almost_equal(npy, arr.asnumpy())
assert_almost_equal(-npy, (-arr).asnumpy())

# 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_almost_equal(npy, arr.asnumpy())
arr_csr = mx.nd.array(npy).to_csr()
arr_rsp = mx.nd.array(npy).to_rsp()
for arr in [arr_csr, arr_rsp]:
assert_almost_equal(npy, arr.asnumpy())
assert_almost_equal(-npy, (-arr).asnumpy())

# 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_almost_equal(npy, arr.asnumpy())


def test_sparse_nd_output_fallback():
Expand Down

0 comments on commit 9d076b5

Please sign in to comment.