Skip to content

Commit

Permalink
Fix remaining errors reported by D2L (apache#16157)
Browse files Browse the repository at this point in the history
* Fix

* Fix ssd

* Add test for np.empty

* Add np.linalg.norm

* Fix indexing bug

* Improve doc
  • Loading branch information
reminisce authored and larroy committed Sep 28, 2019
1 parent 5cbba78 commit 5902b31
Show file tree
Hide file tree
Showing 10 changed files with 751 additions and 62 deletions.
353 changes: 350 additions & 3 deletions python/mxnet/_numpy_op_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@


def _np_ones_like(a):
"""Return an array of ones with the same shape and type as a given array.
"""
ones_like(a)
Return an array of ones with the same shape and type as a given array.
Parameters
----------
Expand All @@ -38,7 +41,10 @@ def _np_ones_like(a):


def _np_zeros_like(a):
"""Return an array of zeros with the same shape and type as a given array.
"""
zeros_like(a)
Return an array of zeros with the same shape and type as a given array.
Parameters
----------
Expand All @@ -55,7 +61,10 @@ def _np_zeros_like(a):


def _np_cumsum(a, axis=None, dtype=None, out=None):
"""Return the cumulative sum of the elements along a given axis.
"""
cumsum(a, axis=None, dtype=None, out=None)
Return the cumulative sum of the elements along a given axis.
Parameters
----------
Expand Down Expand Up @@ -103,3 +112,341 @@ def _np_cumsum(a, axis=None, dtype=None, out=None):
"""
pass


def _np_repeat(a, repeats, axis=None):
"""
repeat(a, repeats, axis=None)
Repeat elements of an array.
Parameters
----------
a : ndarray
Input array.
repeats : int
The number of repetitions for each element.
axis : int, optional
The axis along which to repeat values. By default, use the
flattened input array, and return a flat output array.
Returns
-------
repeated_array : ndarray
Output array which has the same shape as `a`, except along
the given axis.
Notes
-----
Unlike the official NumPy ``repeat`` operator, this operator currently
does not support array of ints for the parameter `repeats`.
Examples
--------
>>> x = np.arange(4).reshape(2, 2)
>>> x
array([[0., 1.],
[2., 3.]])
>>> np.repeat(x, repeats=3)
array([0., 0., 0., 1., 1., 1., 2., 2., 2., 3., 3., 3.])
>>> np.repeat(x, repeats=3, axis=0)
array([[0., 1.],
[0., 1.],
[0., 1.],
[2., 3.],
[2., 3.],
[2., 3.]])
>>> np.repeat(x, repeats=3, axis=1)
array([[0., 0., 0., 1., 1., 1.],
[2., 2., 2., 3., 3., 3.]])
"""
pass


def _np_transpose(a, axes=None):
"""
transpose(a, axes=None)
Permute the dimensions of an array.
Parameters
----------
a : ndarray
Input array.
axes : list of ints, optional
By default, reverse the dimensions,
otherwise permute the axes according to the values given.
Returns
-------
p : ndarray
a with its axes permuted.
Notes
-----
This function differs from the original `numpy.transpose
<https://docs.scipy.org/doc/numpy/reference/generated/numpy.transpose.html>`_ in
the following way(s):
- only ndarray is accepted as valid input, python iterables are not supported
- the operator always returns an `ndarray` that does not share the memory with the input
Examples
--------
>>> x = np.arange(4).reshape((2,2))
>>> x
array([[0., 1.],
[2., 3.]])
>>> np.transpose(x)
array([[0., 2.],
[1., 3.]])
>>> x = np.ones((1, 2, 3))
>>> np.transpose(x, (1, 0, 2)).shape
(2, 1, 3)
"""
pass


def _np_dot(a, b, out=None):
"""dot(a, b, out=None)
Dot product of two arrays. Specifically,
- If both `a` and `b` are 1-D arrays, it is inner product of vectors
- If both `a` and `b` are 2-D arrays, it is matrix multiplication,
- If either `a` or `b` is 0-D (scalar), it is equivalent to :func:`multiply`
and using ``np.multiply(a, b)`` or ``a * b`` is preferred.
- If `a` is an N-D array and `b` is a 1-D array, it is a sum product over
the last axis of `a` and `b`.
- If `a` is an N-D array and `b` is a 2-D array, it is a
sum product over the last axis of `a` and the second-to-last axis of `b`::
dot(a, b)[i,j,k] = sum(a[i,j,:] * b[:,k])
Parameters
----------
a : ndarray
First argument.
b : ndarray
Second argument.
out : ndarray, optional
Output argument. It must have the same shape and type as the expected output.
Returns
-------
output : ndarray
Returns the dot product of `a` and `b`. If `a` and `b` are both
scalars or both 1-D arrays then a scalar is returned; otherwise
an array is returned.
If `out` is given, then it is returned
Examples
--------
>>> a = np.array(3)
>>> b = np.array(4)
>>> np.dot(a, b)
array(12.)
For 2-D arrays it is the matrix product:
>>> a = np.array([[1, 0], [0, 1]])
>>> b = np.array([[4, 1], [2, 2]])
>>> np.dot(a, b)
array([[4., 1.],
[2., 2.]])
>>> a = np.arange(3*4*5*6).reshape((3,4,5,6))
>>> b = np.arange(5*6)[::-1].reshape((6,5))
>>> np.dot(a, b)[2,3,2,2]
array(29884.)
>>> np.sum(a[2,3,2,:] * b[:,2])
array(29884.)
"""
pass


def _np_sum(a, axis=0, dtype=None, keepdims=None, initial=None, out=None):
r"""
sum(a, axis=None, dtype=None, keepdims=_Null, initial=_Null, out=None)
Sum of array elements over a given axis.
Parameters
----------
a : ndarray
Input data.
axis : None or int, optional
Axis or axes along which a sum is performed. The default,
axis=None, will sum all of the elements of the input array. If
axis is negative it counts from the last to the first axis.
dtype : dtype, optional
The type of the returned array and of the accumulator in which the
elements are summed. The default type is float32.
keepdims : bool, optional
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the input array.
If the default value is passed, then `keepdims` will not be
passed through to the `sum` method of sub-classes of
`ndarray`, however any non-default value will be. If the
sub-classes `sum` method does not implement `keepdims` any
exceptions will be raised.
initial: Currently only supports None as input, optional
Starting value for the sum.
Currently not implemented. Please use ``None`` as input or skip this argument.
out : ndarray or None, optional
Alternative output array in which to place the result. It must have
the same shape and dtype as the expected output.
Returns
-------
sum_along_axis : ndarray
An ndarray with the same shape as `a`, with the specified
axis removed. If an output array is specified, a reference to
`out` is returned.
Notes
-----
- Input type does not support Python native iterables.
- "out" param: cannot perform auto type change. out ndarray's dtype must be the same as the expected output.
- "initial" param is not supported yet. Please use None as input.
- Arithmetic is modular when using integer types, and no error is raised on overflow.
- The sum of an empty array is the neutral element 0:
>>> a = np.empty(1)
>>> np.sum(a)
array(0.)
This function differs from the original `numpy.sum
<https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html>`_ in
the following aspects:
- Input type does not support Python native iterables(list, tuple, ...).
- "out" param: cannot perform auto type cast. out ndarray's dtype must be the same as the expected output.
- "initial" param is not supported yet. Please use ``None`` as input or skip it.
Examples
--------
>>> a = np.array([0.5, 1.5])
>>> np.sum(a)
array(2.)
>>> a = np.array([0.5, 0.7, 0.2, 1.5])
>>> np.sum(a, dtype=np.int32)
array(2, dtype=int32)
>>> a = np.array([[0, 1], [0, 5]])
>>> np.sum(a)
array(6.)
>>> np.sum(a, axis=0)
array([0., 6.])
>>> np.sum(a, axis=1)
array([1., 5.])
With output ndarray:
>>> a = np.array([[0, 1], [0, 5]])
>>> b = np.ones((2,), dtype=np.float32)
>>> np.sum(a, axis = 0, out=b)
array([0., 6.])
>>> b
array([0., 6.])
If the accumulator is too small, overflow occurs:
>>> np.ones(128, dtype=np.int8).sum(dtype=np.int8)
array(-128, dtype=int8)
"""
pass


def _np_copy(a, out=None):
"""
copy(a, out=None)
Return an array copy of the given object.
Parameters
----------
a : ndarray
Input data.
out : ndarray or None, optional
Alternative output array in which to place the result. It must have
the same shape and dtype as the expected output.
Returns
-------
arr : ndarray
Array interpretation of `a`.
Notes
-------
This function differs from the original `numpy.copy
<https://docs.scipy.org/doc/numpy/reference/generated/numpy.copy.html>`_ in
the following aspects:
- Input type does not support Python native iterables(list, tuple, ...).
- ``out`` param: cannot perform auto broadcasting. ``out`` ndarray's shape must be the same as the expected output.
- ``out`` param: cannot perform auto type cast. ``out`` ndarray's dtype must be the same as the expected output.
- Does not support "order" parameter.
Examples
--------
Create an array x, with a reference y and a copy z:
>>> x = np.array([1, 2, 3])
>>> y = x
>>> z = np.copy(x)
Note that, when ``x`` is modified, ``y`` is also modified, but not ``z``:
>>> x[0] = 10
>>> x[0] == y[0]
array([1.])
>>> x[0] == z[0]
array([0.])
"""
pass


def _np_reshape(a, newshape, order='C', out=None):
"""
reshape(a, newshape, order='C')
Gives a new shape to an array without changing its data.
This function always returns a copy of the input array if
``out`` is not provided.
Parameters
----------
a : ndarray
Array to be reshaped.
newshape : int or tuple of ints
The new shape should be compatible with the original shape. If
an integer, then the result will be a 1-D array of that length.
One shape dimension can be -1. In this case, the value is
inferred from the length of the array and remaining dimensions.
order : {'C'}, optional
Read the elements of `a` using this index order, and place the
elements into the reshaped array using this index order. 'C'
means to read / write the elements using C-like index order,
with the last axis index changing fastest, back to the first
axis index changing slowest. Other order types such as 'F'/'A'
may be added in the future.
Returns
-------
reshaped_array : ndarray
It will be always a copy of the original array. This behavior is different
from the official NumPy ``reshape`` operator where views of the original array may be
generated.
See Also
--------
ndarray.reshape : Equivalent method.
"""
pass
10 changes: 8 additions & 2 deletions python/mxnet/gluon/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,15 @@ def __init__(self, weight=None, batch_axis=0, **kwargs):

def hybrid_forward(self, F, pred, label, sample_weight=None):
label = _reshape_like(F, label, pred)
loss = F.abs(label - pred)
loss = F.np.abs(label - pred) if is_np_array() else F.abs(label - pred)
loss = _apply_weighting(F, loss, self._weight, sample_weight)
return F.mean(loss, axis=self._batch_axis, exclude=True)
if is_np_array():
if F is ndarray:
return F.np.mean(loss, axis=tuple(range(1, loss.ndim)))
else:
return F.npx.batch_flatten(loss).mean(axis=1)
else:
return F.mean(loss, axis=self._batch_axis, exclude=True)


class SigmoidBinaryCrossEntropyLoss(Loss):
Expand Down
Loading

0 comments on commit 5902b31

Please sign in to comment.