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

Numpy indexing #15763

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
335c858
Moved #13143 changes to numpy branch
zoeygxy Jul 11, 2019
e9e6fec
Fixed np_compat error
zoeygxy Jul 11, 2019
3a9249d
__getitem__ basic indexing finished
zoeygxy Jul 15, 2019
bf925d5
advanced __getitem__ finished, except for cases including None
zoeygxy Jul 21, 2019
2bd7502
setitem basic indexing finished
zoeygxy Jul 23, 2019
83acaf9
Advanced indexing setitem finished
zoeygxy Jul 24, 2019
3685834
Advanced indexing supports None
zoeygxy Jul 26, 2019
d0d492a
Fixed logic in basic indexing setitem and new_axes, and advanced inde…
zoeygxy Aug 2, 2019
078ab55
Before rebase
zoeygxy Aug 2, 2019
5ec9a25
Doc for full and slice_assign
zoeygxy Aug 5, 2019
a7d4056
Style fixed
zoeygxy Aug 6, 2019
4b6e18a
moved numpy indexing test to tets_numpy_ndarray.py
zoeygxy Aug 6, 2019
82050d0
Indexing test reorganized
zoeygxy Aug 7, 2019
e01c829
python2 fix
zoeygxy Aug 7, 2019
50951d6
Fixed np.array() for zero-size inputs
zoeygxy Aug 8, 2019
94d1599
Typo in test_operator fixed; temporary gpu support for npx.slice
zoeygxy Aug 8, 2019
e21c2fa
Support zero-sized tensor get and set
zoeygxy Aug 8, 2019
078af69
fixed zero-size tensor setitem
zoeygxy Aug 9, 2019
d9dc6e5
further fix zero-size tensor
zoeygxy Aug 9, 2019
78013b9
Fix scalar tensor setitem precision
zoeygxy Aug 12, 2019
d815d9b
fixed settitem output zero-size tensor for NDArray without numpy shape
zoeygxy Aug 13, 2019
6752f2f
Added handling for x[1:1]=y; fixed some zero-size tensor autograd
zoeygxy Aug 13, 2019
9639e7b
Fixed grad for zero-sized tensor
zoeygxy Aug 14, 2019
bb6e119
Retrigger CI
zoeygxy Aug 14, 2019
e85d83c
Retrigger CI
zoeygxy Aug 14, 2019
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
1,352 changes: 898 additions & 454 deletions python/mxnet/ndarray/ndarray.py

Large diffs are not rendered by default.

78 changes: 76 additions & 2 deletions python/mxnet/ndarray/numpy/_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
from . import _internal as _npi
from ..ndarray import NDArray

__all__ = ['zeros', 'ones', 'maximum', 'minimum', 'stack', 'arange', 'argmax',
__all__ = ['zeros', 'ones', 'full', 'maximum', 'minimum', 'stack', 'arange', 'argmax',
'add', 'subtract', 'multiply', 'divide', 'mod', 'power', 'concatenate',
'clip', 'split', 'swapaxes', 'expand_dims', 'tile', 'linspace', 'eye',
'sin', 'cos', 'sinh', 'cosh', 'log10', 'sqrt', 'abs', 'exp', 'arctan', 'sign', 'log',
'degrees', 'log2', 'rint', 'radians', 'mean', 'reciprocal', 'square', 'arcsin',
'argsort', 'hstack', 'tensordot']
'argsort', 'hstack', 'tensordot', "slice_assign_scalar", "slice_assign"]


@set_module('mxnet.ndarray.numpy')
Expand Down Expand Up @@ -178,6 +178,62 @@ def ones(shape, dtype=None, **kwargs):
return _npi.ones(shape=shape, ctx=ctx, dtype=dtype, **kwargs)


@set_module('mxnet.ndarray.numpy')
def full(shape, fill_value, dtype=None, ctx=None, out=None):
"""
Return a new array of given shape and type, filled with `fill_value`.

Parameters
----------
shape : int or sequence of ints
Shape of the new array, e.g., ``(2, 3)`` or ``2``.
fill_value : scalar
Fill value.
dtype : data-type, optional
The desired data-type for the array. The default, `None`, means
`np.array(fill_value).dtype`.
ctx: to specify the device, e.g. the i-th GPU.
out : ndarray or None, optional
A location into which the result is stored.
If provided, it must have the same shape and dtype as input ndarray.
If not provided or `None`, a freshly-allocated array is returned.

Returns
-------
out : ndarray
Array of `fill_value` with the given shape, dtype, and order.

Notes
-----
This function differs from the original `numpy.full
https://docs.scipy.org/doc/numpy/reference/generated/numpy.full.html`_ in
the following way(s):

- Have an additional `ctx` argument to specify the device
- Have an additional `out` argument
- Currently does not support `order` selection

See Also
--------
empty : Return a new uninitialized array.
ones : Return a new array setting values to one.
zeros : Return a new array setting values to zero.

Examples
--------
>>> np.full((2, 2), 10)
array([[10., 10.],
[10., 10.]])
>>> np.full((2, 2), 2, dtype=np.int32, ctx=mx.cpu(0))
array([[2, 2],
[2, 2]], dtype=int32)
"""
if ctx is None:
ctx = current_context()
dtype = _np.float32 if dtype is None else dtype
return _npi.full(shape=shape, value=fill_value, ctx=ctx, dtype=dtype, out=out)


#pylint: disable= too-many-arguments, no-member, protected-access
def _ufunc_helper(lhs, rhs, fn_array, fn_scalar, lfn_scalar, rfn_scalar=None, out=None):
""" Helper function for element-wise operation.
Expand Down Expand Up @@ -1902,3 +1958,21 @@ def arcsin(x, out=None, **kwargs):
http://www.math.sfu.ca/~cbm/aands/
"""
return _unary_func_helper(x, _npi.arcsin, _np.arcsin, out=out, **kwargs)

@set_module('mxnet.ndarray.numpy')
def slice_assign_scalar(lhs, value, begin, end, step, out=None): # pylint: disable=too-many-arguments
"""
Assign the rhs to a cropped subset of this ndarray in place. Returns the view of this ndarray.
`rhs` and `lhs` should be of the same data type, and on the same device.
The shape of rhs should be the same as the cropped shape of this ndarray.
"""
_npi.slice_assign_scalar(lhs, value, begin=begin, end=end, step=step, out=out)


@set_module('mxnet.ndarray.numpy')
def slice_assign(lhs, rhs, begin, end, step, out=None): # pylint: disable=too-many-arguments
"""
Assign the scalar to a cropped subset of this ndarray. Value will broadcast to the shape of the
cropped shape and will be cast to the same dtype of the ndarray.
"""
return _npi.slice_assign(lhs, rhs, begin=begin, end=end, step=step, out=out)
Loading