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

Commit cb67e82

Browse files
author
Ying
committed
numpy operator ravel, derive from reshape
* it is the same as reshape(x, -1) * register reshape with prefix _npi_ * fix format error * edit examples in doc * fix error in review * add out in wrapper * remove out * test data type and add order * change order check * remove redundant test and add docstring about order
1 parent 287e3b5 commit cb67e82

File tree

5 files changed

+175
-3
lines changed

5 files changed

+175
-3
lines changed

python/mxnet/ndarray/numpy/_op.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
'rint', 'radians', 'reciprocal', 'square', 'negative', 'fix', 'ceil', 'floor',
3434
'trunc', 'logical_not', 'arcsinh', 'arccosh', 'arctanh', 'tensordot',
3535
'linspace', 'expand_dims', 'tile', 'arange', 'split', 'concatenate', 'stack', 'mean',
36-
'maximum', 'minimum', 'swapaxes', 'clip', 'argmax', 'std', 'var', 'indices']
36+
'maximum', 'minimum', 'swapaxes', 'clip', 'argmax', 'std', 'var', 'indices', 'ravel']
3737

3838

3939
@set_module('mxnet.ndarray.numpy')
@@ -2432,3 +2432,56 @@ def indices(dimensions, dtype=_np.int32, ctx=None):
24322432
else:
24332433
raise ValueError("The dimensions must be sequence of ints")
24342434
# pylint: enable=redefined-outer-name
2435+
2436+
2437+
@set_module('mxnet.ndarray.numpy')
2438+
def ravel(x, order='C'):
2439+
r"""
2440+
ravel(x)
2441+
2442+
Return a contiguous flattened array.
2443+
A 1-D array, containing the elements of the input, is returned. A copy is
2444+
made only if needed.
2445+
2446+
Parameters
2447+
----------
2448+
x : ndarray
2449+
Input array. The elements in `x` are read in row-major, C-style order and
2450+
packed as a 1-D array.
2451+
order : `C`, optional
2452+
Only support row-major, C-style order.
2453+
2454+
Returns
2455+
-------
2456+
y : ndarray
2457+
y is an array of the same subtype as `x`, with shape ``(x.size,)``.
2458+
Note that matrices are special cased for backward compatibility, if `x`
2459+
is a matrix, then y is a 1-D ndarray.
2460+
2461+
Notes
2462+
-----
2463+
This function differs from the original numpy.arange in the following aspects:
2464+
- Only support row-major, C-style order.
2465+
2466+
Examples
2467+
--------
2468+
It is equivalent to ``reshape(x, -1)``.
2469+
2470+
>>> x = np.array([[1, 2, 3], [4, 5, 6]])
2471+
>>> print(np.ravel(x))
2472+
[1. 2. 3. 4. 5. 6.]
2473+
2474+
>>> print(x.reshape(-1))
2475+
[1. 2. 3. 4. 5. 6.]
2476+
2477+
>>> print(np.ravel(x.T))
2478+
[1. 4. 2. 5. 3. 6.]
2479+
"""
2480+
if order != 'C':
2481+
raise NotImplementedError('order {} is not supported'.format(order))
2482+
if isinstance(x, numeric_types):
2483+
return _np.reshape(x, -1)
2484+
elif isinstance(x, NDArray):
2485+
return _npi.reshape(x, -1)
2486+
else:
2487+
raise TypeError('type {} not supported'.format(str(type(x))))

python/mxnet/numpy/multiarray.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
'degrees', 'log2', 'log1p', 'rint', 'radians', 'reciprocal', 'square', 'negative',
5353
'fix', 'ceil', 'floor', 'trunc', 'logical_not', 'arcsinh', 'arccosh', 'arctanh',
5454
'tensordot', 'linspace', 'expand_dims', 'tile', 'arange', 'split', 'concatenate',
55-
'stack', 'mean', 'maximum', 'minimum', 'swapaxes', 'clip', 'argmax', 'std', 'var', 'indices']
55+
'stack', 'mean', 'maximum', 'minimum', 'swapaxes', 'clip', 'argmax', 'std', 'var', 'indices', 'ravel']
5656

5757
# Return code for dispatching indexing function call
5858
_NDARRAY_UNSUPPORTED_INDEXING = -1
@@ -3873,3 +3873,49 @@ def indices(dimensions, dtype=_np.int32, ctx=None):
38733873
"""
38743874
return _mx_nd_np.indices(dimensions=dimensions, dtype=dtype, ctx=ctx)
38753875
# pylint: enable=redefined-outer-name
3876+
3877+
3878+
@set_module('mxnet.numpy')
3879+
def ravel(x, order='C'):
3880+
r"""
3881+
ravel(x)
3882+
3883+
Return a contiguous flattened array.
3884+
A 1-D array, containing the elements of the input, is returned. A copy is
3885+
made only if needed.
3886+
3887+
Parameters
3888+
----------
3889+
x : ndarray
3890+
Input array. The elements in `x` are read in row-major, C-style order and
3891+
packed as a 1-D array.
3892+
order : `C`, optional
3893+
Only support row-major, C-style order.
3894+
3895+
Returns
3896+
-------
3897+
y : ndarray
3898+
y is an array of the same subtype as `x`, with shape ``(x.size,)``.
3899+
Note that matrices are special cased for backward compatibility, if `x`
3900+
is a matrix, then y is a 1-D ndarray.
3901+
3902+
Notes
3903+
-----
3904+
This function differs from the original numpy.arange in the following aspects:
3905+
- Only support row-major, C-style order.
3906+
3907+
Examples
3908+
--------
3909+
It is equivalent to ``reshape(x, -1)``.
3910+
3911+
>>> x = np.array([[1, 2, 3], [4, 5, 6]])
3912+
>>> print(np.ravel(x))
3913+
[1. 2. 3. 4. 5. 6.]
3914+
3915+
>>> print(x.reshape(-1))
3916+
[1. 2. 3. 4. 5. 6.]
3917+
3918+
>>> print(np.ravel(x.T))
3919+
[1. 4. 2. 5. 3. 6.]
3920+
"""
3921+
return _mx_nd_np.ravel(x, order)

python/mxnet/symbol/numpy/_symbol.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
'rint', 'radians', 'reciprocal', 'square', 'negative', 'fix', 'ceil', 'floor',
3636
'trunc', 'logical_not', 'arcsinh', 'arccosh', 'arctanh', 'tensordot',
3737
'linspace', 'expand_dims', 'tile', 'arange', 'split', 'concatenate', 'stack', 'mean',
38-
'maximum', 'minimum', 'swapaxes', 'clip', 'argmax', 'std', 'var', 'indices']
38+
'maximum', 'minimum', 'swapaxes', 'clip', 'argmax', 'std', 'var', 'indices', 'ravel']
3939

4040

4141
def _num_outputs(sym):
@@ -2748,4 +2748,43 @@ def indices(dimensions, dtype=_np.int32, ctx=None):
27482748
# pylint: enable=redefined-outer-name
27492749

27502750

2751+
@set_module('mxnet.symbol.numpy')
2752+
def ravel(x, order='C'):
2753+
r"""
2754+
ravel(x)
2755+
2756+
Return a contiguous flattened array.
2757+
A 1-D array, containing the elements of the input, is returned. A copy is
2758+
made only if needed.
2759+
2760+
Parameters
2761+
----------
2762+
x : ndarray
2763+
Input array. The elements in `x` are read in row-major, C-style order and
2764+
packed as a 1-D array.
2765+
order : `C`, optional
2766+
Only support row-major, C-style order.
2767+
2768+
Returns
2769+
-------
2770+
y : ndarray
2771+
y is an array of the same subtype as `x`, with shape ``(x.size,)``.
2772+
Note that matrices are special cased for backward compatibility, if `x`
2773+
is a matrix, then y is a 1-D ndarray.
2774+
2775+
Notes
2776+
-----
2777+
This function differs from the original numpy.arange in the following aspects:
2778+
- Only support row-major, C-style order.
2779+
"""
2780+
if order != 'C':
2781+
raise NotImplementedError('order {} is not supported'.format(order))
2782+
if isinstance(x, numeric_types):
2783+
return _np.reshape(x, -1)
2784+
elif isinstance(x, _Symbol):
2785+
return _npi.reshape(x, -1)
2786+
else:
2787+
raise TypeError('type {} not supported'.format(str(type(x))))
2788+
2789+
27512790
_set_np_symbol_class(_Symbol)

src/operator/numpy/np_matrix_op.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ bool NumpyReshapeShape(const nnvm::NodeAttrs& attrs,
163163

164164
NNVM_REGISTER_OP(_np_reshape)
165165
.describe(R"code()code" ADD_FILELINE)
166+
.add_alias("_npi_reshape")
166167
.set_num_inputs(1)
167168
.set_num_outputs(1)
168169
.set_attr_parser(ParamParser<NumpyReshapeParam>)

tests/python/unittest/test_numpy_op.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,39 @@ def hybrid_forward(self, F, a, *args):
13241324
assert same(mx_out.asnumpy(), np_out)
13251325

13261326

1327+
@with_seed()
1328+
@use_np
1329+
def test_np_ravel():
1330+
class TestRavel(HybridBlock):
1331+
def __init__(self):
1332+
super(TestRavel, self).__init__()
1333+
1334+
def hybrid_forward(self, F, a):
1335+
return F.np.ravel(a)
1336+
1337+
types = ['float64', 'float32', 'float16', 'int64', 'int32', 'int8']
1338+
for oneType in types:
1339+
for hybridize in [True, False]:
1340+
for shape in [(), (2,), (2, 2), (1, 2, 3), (3, 0), (1, 0, 2)]:
1341+
test_ravel = TestRavel()
1342+
if hybridize:
1343+
test_ravel.hybridize()
1344+
x = rand_ndarray(shape, dtype=oneType).as_np_ndarray()
1345+
x.attach_grad()
1346+
np_out = _np.ravel(x.asnumpy())
1347+
with mx.autograd.record():
1348+
mx_out = test_ravel(x)
1349+
assert mx_out.shape == np_out.shape
1350+
assert_almost_equal(mx_out.asnumpy(), np_out, rtol=1e-3, atol=1e-5)
1351+
mx_out.backward()
1352+
np_backward = _np.ones(shape)
1353+
assert_almost_equal(x.grad.asnumpy(), np_backward, rtol=1e-3, atol=1e-5)
1354+
1355+
mx_out = np.ravel(x)
1356+
np_out = _np.ravel(x.asnumpy())
1357+
assert_almost_equal(mx_out.asnumpy(), np_out, rtol=1e-3, atol=1e-5)
1358+
1359+
13271360
@with_seed()
13281361
@use_np
13291362
def test_np_randint():

0 commit comments

Comments
 (0)