Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Showing proper error when csr array is not 2D in shape. #15242

Merged
merged 3 commits into from
Jun 20, 2019
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
4 changes: 4 additions & 0 deletions python/mxnet/ndarray/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2227,6 +2227,10 @@ def tostype(self, stype):
NDArray, CSRNDArray or RowSparseNDArray
A copy of the array with the chosen storage stype
"""
if stype == 'csr' and len(self.shape) != 2:
raise ValueError("To convert to a CSR, the NDArray should be 2 Dimensional. Current "
"shape is %s" % str(self.shape))

return op.cast_storage(self, stype=stype)

def to_dlpack_for_read(self):
Expand Down
5 changes: 5 additions & 0 deletions tests/python/unittest/test_sparse_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,11 @@ def test_sparse_nd_check_format():
indptr_list = [0, -2, 2, 3]
a = mx.nd.sparse.csr_matrix((data_list, indices_list, indptr_list), shape=shape)
assertRaises(mx.base.MXNetError, a.check_format)
# CSR format should be 2 Dimensional.
a = mx.nd.array([1, 2, 3])
assertRaises(ValueError, a.tostype, 'csr')
a = mx.nd.array([[[1, 2, 3]]])
assertRaises(ValueError, a.tostype, 'csr')
# Row Sparse format indices should be less than the number of rows
shape = (3, 2)
data_list = [[1, 2], [3, 4]]
Expand Down