-
Notifications
You must be signed in to change notification settings - Fork 2
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
Code changes based on reviews #144
Changes from 5 commits
e4ddaab
c15ca3c
5fd9740
451a8b4
39fac9b
43bbbb8
e3ccf32
1dd4faa
26b821e
1aaf999
ccd1836
a9e7980
25d51f9
8f54803
351724d
79fa6f6
dcf1bb7
0569955
87db8fe
ccc4953
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,7 +217,9 @@ def _aux_data(self, i): | |
|
||
# pylint: disable=abstract-method | ||
class CSRNDArray(BaseSparseNDArray): | ||
"""A CSRNDArray represents a NDArray as three separate arrays: `data`, | ||
"""A sparse representation of tensor in standard CSR format. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to change Also, "in the standard CSR format." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about |
||
|
||
A CSRNDArray represents an NDArray as three separate arrays: `data`, | ||
`indptr` and `indices`. It uses the standard CSR representation where the column indices for | ||
row i are stored in indices[indptr[i]:indptr[i+1]] and their corresponding values are stored | ||
in values[indptr[i]:indptr[i+1]]. | ||
|
@@ -269,13 +271,16 @@ def __getitem__(self, key): | |
|
||
Examples | ||
-------- | ||
>>> x = mx.nd.zeros((2, 3), stype='csr') | ||
>>> x[:] = mx.nd.arange(0,6).reshape((2,3)) | ||
>>> x.asnumpy() | ||
array([[ 0., 1., 2.], | ||
[ 3., 4., 5.]], dtype=float32) | ||
>>> x[1:2].asnumpy() | ||
array([[ 3., 4., 5.]], dtype=float32) | ||
>>> indptr = np.array([0, 2, 3, 6]) | ||
>>> indices = np.array([0, 2, 2, 0, 1, 2]) | ||
>>> data = np.array([1, 2, 3, 4, 5, 6]) | ||
>>> a = mx.nd.csr_matrix(data, indptr, indices, (3, 3)) | ||
>>> a.asnumpy() | ||
array([[1, 0, 2], | ||
[0, 0, 3], | ||
[4, 5, 6]]) | ||
>>> a[1:2].asnumpy() | ||
array([[0, 0, 3]], dtype=float32) | ||
""" | ||
if isinstance(key, int): | ||
raise ValueError("__getitem__ with int key is not implemented for CSRNDArray") | ||
|
@@ -420,26 +425,34 @@ def copyto(self, other): | |
|
||
# pylint: disable=abstract-method | ||
class RowSparseNDArray(BaseSparseNDArray): | ||
"""A RowSparseNDArray is typically used to represent a subset of a larger | ||
NDArray with `default` of shape [LARGE0, D1, .. , DN] where LARGE0 >> D0. The values | ||
in indices are the indices in the first dimension of the slices that have been extracted from | ||
the larger NDArray. The indices are expected to be sorted in ascending order. | ||
"""A sparse representation of a set of tensor slices at given indices. | ||
|
||
The corresponding NDArray ``dense`` with `default` storage represented by a ``rsp`` | ||
RowSparseNDArray | ||
A RowSparseNDArray represents an K-dimensional NDArray as two separate arrays: `data` and | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
`indices`. The `indices` stores the indices of the K-dimensional `data` slices extracted | ||
from the dense NDArray in the first dimension. The corresponding NDArray ``dense`` | ||
represented by RowSparseNDArray ``rsp`` has | ||
|
||
``dense[rsp.indices[i], :, :, :, ...] = rsp.data[i, :, :, :, ...]``, | ||
|
||
``dense[rsp.indices[i], :, :, :, ...] = rsp.values[i, :, :, :, ...]`` | ||
where `indices` is an 1-D integer NDArray with shape [D0], and `data` is an NDArray of any | ||
dtype with shape [D0, D1, .., DK]. If the index of a slice in the first dimension | ||
doesn't appear in `indices`, its values are zeros. | ||
|
||
A RowSparseNDArray is typically used to represent a subset of a larger dense NDArray of | ||
shape [LARGE0, D1, .. , DK] where LARGE0 >> D0 and most row slices are zeros. | ||
|
||
The indices are expected to be sorted in ascending order. | ||
|
||
RowSparseNDArray is used principally in the definition of gradients for operations | ||
that have sparse gradients (e.g. dot with sparse inputs). | ||
|
||
Examples | ||
-------- | ||
>>> import mxnet as mx | ||
>>> dense = mx.nd.array([[1,2],[0,0],[3,0],[0,0]]) | ||
>>> dense = mx.nd.array([[1,2],[0,0],[3,0],[0,0],[0,0],[0,0]]) | ||
>>> rsp = dense._to_rsp() | ||
>>> rsp.indices.asnumpy() | ||
array([0, 2], dtype=int32) | ||
array([0, 2], dtype=int64) | ||
>>> rsp.data.asnumpy() | ||
array([[ 1., 2.], | ||
[ 3., 0.]], dtype=float32) | ||
|
@@ -608,6 +621,9 @@ def copyto(self, other): | |
raise TypeError('copyto does not support type ' + str(type(other))) | ||
|
||
def _prepare_src_array(src, dtype, default_dtype): | ||
"""Prepare `src` and its dtype so that they can be used to construct NDArray. | ||
`src` is converted to a `np.ndarray` if it's neither an `NDArray` nor an `np.ndarray`. | ||
""" | ||
if isinstance(src, NDArray): | ||
dtype = src.dtype if dtype is None else dtype | ||
else: | ||
|
@@ -620,8 +636,9 @@ def _prepare_src_array(src, dtype, default_dtype): | |
return src, dtype | ||
|
||
|
||
def csr(data, indptr, indices, shape, ctx=None, dtype=None, indptr_type=None, indices_type=None): | ||
"""Creates a 2D array with compressed sparse row format. | ||
def csr_matrix(data, indptr, indices, shape, ctx=None, dtype=None, indptr_type=None, | ||
indices_type=None): | ||
"""Creates a 2D array with compressed sparse row(CSR) format. | ||
|
||
Parameters | ||
---------- | ||
|
@@ -640,10 +657,10 @@ def csr(data, indptr, indices, shape, ctx=None, dtype=None, indptr_type=None, in | |
if `values` is an `NDArray`, `float32` otherwise. | ||
indptr_type: str or numpy.dtype, optional | ||
The data type of the indices array. The default dtype is ``indptr.dtype`` | ||
if `indptr` is an `NDArray`, `int32` otherwise. | ||
if `indptr` is an `NDArray`, `int64` otherwise. | ||
indices_type: str or numpy.dtype, optional | ||
The data type of the indices array. The default dtype is ``indices.dtype`` | ||
if `indicies` is an `NDArray`, `int32` otherwise. | ||
if `indicies` is an `NDArray`, `int64` otherwise. | ||
|
||
Returns | ||
------- | ||
|
@@ -653,7 +670,7 @@ def csr(data, indptr, indices, shape, ctx=None, dtype=None, indptr_type=None, in | |
Example | ||
------- | ||
>>> import mxnet as mx | ||
>>> a = mx.nd.csr([1, 2, 3], [0, 1, 2, 2, 3], [1, 0, 2], (4, 3)) | ||
>>> a = mx.nd.csr_matrix([1, 2, 3], [0, 1, 2, 2, 3], [1, 0, 2], (4, 3)) | ||
>>> a.asnumpy() | ||
array([[ 0., 1., 0.], | ||
[ 2., 0., 0.], | ||
|
@@ -696,13 +713,13 @@ def csr(data, indptr, indices, shape, ctx=None, dtype=None, indptr_type=None, in | |
return result | ||
|
||
|
||
def row_sparse(data, indices, shape, ctx=None, dtype=None, indices_type=None): | ||
"""Creates a row sparse array with a set of tensor slices at given indices. | ||
def row_sparse_array(data, indices, shape, ctx=None, dtype=None, indices_type=None): | ||
"""Creates a K-dimensional row sparse array with a set of tensor slices at given indices. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use "multidimensional"? |
||
|
||
Parameters | ||
---------- | ||
data: array_like | ||
An object exposing the array interface, with shape [D0, D1, .. Dn], where D0 is | ||
An object exposing the array interface, with shape [D0, D1, .. DK], where D0 is | ||
the number of rows with non-zeros entries. | ||
indices: array_like | ||
An object exposing the array interface, with shape [D0]. | ||
|
@@ -713,7 +730,7 @@ def row_sparse(data, indices, shape, ctx=None, dtype=None, indices_type=None): | |
if `data` is an `NDArray`, `float32` otherwise. | ||
indices_type: str or numpy.dtype, optional | ||
The data type of the indices array. The default dtype is ``indices.dtype`` | ||
if `indicies` is an `NDArray`, `int32` otherwise. | ||
if `indicies` is an `NDArray`, `int64` otherwise. | ||
|
||
Returns | ||
------- | ||
|
@@ -722,7 +739,7 @@ def row_sparse(data, indices, shape, ctx=None, dtype=None, indices_type=None): | |
|
||
Example | ||
------- | ||
>>> a = mx.nd.row_sparse([[1, 2], [3, 4]], [1, 4], (6, 2)) | ||
>>> a = mx.nd.row_sparse_array([[1, 2], [3, 4]], [1, 4], (6, 2)) | ||
>>> a.asnumpy() | ||
array([[ 0., 0.], | ||
[ 1., 2.], | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not return const reference and avoid construction?