Skip to content

Commit 5902b31

Browse files
reminiscelarroy
authored andcommitted
Fix remaining errors reported by D2L (apache#16157)
* Fix * Fix ssd * Add test for np.empty * Add np.linalg.norm * Fix indexing bug * Improve doc
1 parent 5cbba78 commit 5902b31

File tree

10 files changed

+751
-62
lines changed

10 files changed

+751
-62
lines changed

python/mxnet/_numpy_op_doc.py

Lines changed: 350 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121

2222

2323
def _np_ones_like(a):
24-
"""Return an array of ones with the same shape and type as a given array.
24+
"""
25+
ones_like(a)
26+
27+
Return an array of ones with the same shape and type as a given array.
2528
2629
Parameters
2730
----------
@@ -38,7 +41,10 @@ def _np_ones_like(a):
3841

3942

4043
def _np_zeros_like(a):
41-
"""Return an array of zeros with the same shape and type as a given array.
44+
"""
45+
zeros_like(a)
46+
47+
Return an array of zeros with the same shape and type as a given array.
4248
4349
Parameters
4450
----------
@@ -55,7 +61,10 @@ def _np_zeros_like(a):
5561

5662

5763
def _np_cumsum(a, axis=None, dtype=None, out=None):
58-
"""Return the cumulative sum of the elements along a given axis.
64+
"""
65+
cumsum(a, axis=None, dtype=None, out=None)
66+
67+
Return the cumulative sum of the elements along a given axis.
5968
6069
Parameters
6170
----------
@@ -103,3 +112,341 @@ def _np_cumsum(a, axis=None, dtype=None, out=None):
103112
104113
"""
105114
pass
115+
116+
117+
def _np_repeat(a, repeats, axis=None):
118+
"""
119+
repeat(a, repeats, axis=None)
120+
121+
Repeat elements of an array.
122+
123+
Parameters
124+
----------
125+
a : ndarray
126+
Input array.
127+
repeats : int
128+
The number of repetitions for each element.
129+
axis : int, optional
130+
The axis along which to repeat values. By default, use the
131+
flattened input array, and return a flat output array.
132+
133+
Returns
134+
-------
135+
repeated_array : ndarray
136+
Output array which has the same shape as `a`, except along
137+
the given axis.
138+
139+
Notes
140+
-----
141+
Unlike the official NumPy ``repeat`` operator, this operator currently
142+
does not support array of ints for the parameter `repeats`.
143+
144+
Examples
145+
--------
146+
>>> x = np.arange(4).reshape(2, 2)
147+
>>> x
148+
array([[0., 1.],
149+
[2., 3.]])
150+
>>> np.repeat(x, repeats=3)
151+
array([0., 0., 0., 1., 1., 1., 2., 2., 2., 3., 3., 3.])
152+
>>> np.repeat(x, repeats=3, axis=0)
153+
array([[0., 1.],
154+
[0., 1.],
155+
[0., 1.],
156+
[2., 3.],
157+
[2., 3.],
158+
[2., 3.]])
159+
>>> np.repeat(x, repeats=3, axis=1)
160+
array([[0., 0., 0., 1., 1., 1.],
161+
[2., 2., 2., 3., 3., 3.]])
162+
"""
163+
pass
164+
165+
166+
def _np_transpose(a, axes=None):
167+
"""
168+
transpose(a, axes=None)
169+
170+
Permute the dimensions of an array.
171+
172+
Parameters
173+
----------
174+
a : ndarray
175+
Input array.
176+
axes : list of ints, optional
177+
By default, reverse the dimensions,
178+
otherwise permute the axes according to the values given.
179+
180+
Returns
181+
-------
182+
p : ndarray
183+
a with its axes permuted.
184+
185+
Notes
186+
-----
187+
This function differs from the original `numpy.transpose
188+
<https://docs.scipy.org/doc/numpy/reference/generated/numpy.transpose.html>`_ in
189+
the following way(s):
190+
191+
- only ndarray is accepted as valid input, python iterables are not supported
192+
- the operator always returns an `ndarray` that does not share the memory with the input
193+
194+
Examples
195+
--------
196+
>>> x = np.arange(4).reshape((2,2))
197+
>>> x
198+
array([[0., 1.],
199+
[2., 3.]])
200+
>>> np.transpose(x)
201+
array([[0., 2.],
202+
[1., 3.]])
203+
>>> x = np.ones((1, 2, 3))
204+
>>> np.transpose(x, (1, 0, 2)).shape
205+
(2, 1, 3)
206+
"""
207+
pass
208+
209+
210+
def _np_dot(a, b, out=None):
211+
"""dot(a, b, out=None)
212+
213+
Dot product of two arrays. Specifically,
214+
215+
- If both `a` and `b` are 1-D arrays, it is inner product of vectors
216+
217+
- If both `a` and `b` are 2-D arrays, it is matrix multiplication,
218+
219+
- If either `a` or `b` is 0-D (scalar), it is equivalent to :func:`multiply`
220+
and using ``np.multiply(a, b)`` or ``a * b`` is preferred.
221+
222+
- If `a` is an N-D array and `b` is a 1-D array, it is a sum product over
223+
the last axis of `a` and `b`.
224+
225+
- If `a` is an N-D array and `b` is a 2-D array, it is a
226+
sum product over the last axis of `a` and the second-to-last axis of `b`::
227+
228+
dot(a, b)[i,j,k] = sum(a[i,j,:] * b[:,k])
229+
230+
Parameters
231+
----------
232+
a : ndarray
233+
First argument.
234+
b : ndarray
235+
Second argument.
236+
237+
out : ndarray, optional
238+
Output argument. It must have the same shape and type as the expected output.
239+
240+
Returns
241+
-------
242+
output : ndarray
243+
Returns the dot product of `a` and `b`. If `a` and `b` are both
244+
scalars or both 1-D arrays then a scalar is returned; otherwise
245+
an array is returned.
246+
If `out` is given, then it is returned
247+
248+
Examples
249+
--------
250+
>>> a = np.array(3)
251+
>>> b = np.array(4)
252+
>>> np.dot(a, b)
253+
array(12.)
254+
255+
For 2-D arrays it is the matrix product:
256+
257+
>>> a = np.array([[1, 0], [0, 1]])
258+
>>> b = np.array([[4, 1], [2, 2]])
259+
>>> np.dot(a, b)
260+
array([[4., 1.],
261+
[2., 2.]])
262+
263+
>>> a = np.arange(3*4*5*6).reshape((3,4,5,6))
264+
>>> b = np.arange(5*6)[::-1].reshape((6,5))
265+
>>> np.dot(a, b)[2,3,2,2]
266+
array(29884.)
267+
>>> np.sum(a[2,3,2,:] * b[:,2])
268+
array(29884.)
269+
"""
270+
pass
271+
272+
273+
def _np_sum(a, axis=0, dtype=None, keepdims=None, initial=None, out=None):
274+
r"""
275+
sum(a, axis=None, dtype=None, keepdims=_Null, initial=_Null, out=None)
276+
277+
Sum of array elements over a given axis.
278+
279+
Parameters
280+
----------
281+
a : ndarray
282+
Input data.
283+
axis : None or int, optional
284+
Axis or axes along which a sum is performed. The default,
285+
axis=None, will sum all of the elements of the input array. If
286+
axis is negative it counts from the last to the first axis.
287+
dtype : dtype, optional
288+
The type of the returned array and of the accumulator in which the
289+
elements are summed. The default type is float32.
290+
keepdims : bool, optional
291+
If this is set to True, the axes which are reduced are left
292+
in the result as dimensions with size one. With this option,
293+
the result will broadcast correctly against the input array.
294+
295+
If the default value is passed, then `keepdims` will not be
296+
passed through to the `sum` method of sub-classes of
297+
`ndarray`, however any non-default value will be. If the
298+
sub-classes `sum` method does not implement `keepdims` any
299+
exceptions will be raised.
300+
initial: Currently only supports None as input, optional
301+
Starting value for the sum.
302+
Currently not implemented. Please use ``None`` as input or skip this argument.
303+
out : ndarray or None, optional
304+
Alternative output array in which to place the result. It must have
305+
the same shape and dtype as the expected output.
306+
307+
Returns
308+
-------
309+
sum_along_axis : ndarray
310+
An ndarray with the same shape as `a`, with the specified
311+
axis removed. If an output array is specified, a reference to
312+
`out` is returned.
313+
314+
Notes
315+
-----
316+
- Input type does not support Python native iterables.
317+
- "out" param: cannot perform auto type change. out ndarray's dtype must be the same as the expected output.
318+
- "initial" param is not supported yet. Please use None as input.
319+
- Arithmetic is modular when using integer types, and no error is raised on overflow.
320+
- The sum of an empty array is the neutral element 0:
321+
322+
>>> a = np.empty(1)
323+
>>> np.sum(a)
324+
array(0.)
325+
326+
This function differs from the original `numpy.sum
327+
<https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html>`_ in
328+
the following aspects:
329+
330+
- Input type does not support Python native iterables(list, tuple, ...).
331+
- "out" param: cannot perform auto type cast. out ndarray's dtype must be the same as the expected output.
332+
- "initial" param is not supported yet. Please use ``None`` as input or skip it.
333+
334+
Examples
335+
--------
336+
>>> a = np.array([0.5, 1.5])
337+
>>> np.sum(a)
338+
array(2.)
339+
>>> a = np.array([0.5, 0.7, 0.2, 1.5])
340+
>>> np.sum(a, dtype=np.int32)
341+
array(2, dtype=int32)
342+
>>> a = np.array([[0, 1], [0, 5]])
343+
>>> np.sum(a)
344+
array(6.)
345+
>>> np.sum(a, axis=0)
346+
array([0., 6.])
347+
>>> np.sum(a, axis=1)
348+
array([1., 5.])
349+
350+
With output ndarray:
351+
352+
>>> a = np.array([[0, 1], [0, 5]])
353+
>>> b = np.ones((2,), dtype=np.float32)
354+
>>> np.sum(a, axis = 0, out=b)
355+
array([0., 6.])
356+
>>> b
357+
array([0., 6.])
358+
359+
If the accumulator is too small, overflow occurs:
360+
361+
>>> np.ones(128, dtype=np.int8).sum(dtype=np.int8)
362+
array(-128, dtype=int8)
363+
"""
364+
pass
365+
366+
367+
def _np_copy(a, out=None):
368+
"""
369+
copy(a, out=None)
370+
371+
Return an array copy of the given object.
372+
373+
Parameters
374+
----------
375+
a : ndarray
376+
Input data.
377+
out : ndarray or None, optional
378+
Alternative output array in which to place the result. It must have
379+
the same shape and dtype as the expected output.
380+
381+
Returns
382+
-------
383+
arr : ndarray
384+
Array interpretation of `a`.
385+
386+
Notes
387+
-------
388+
This function differs from the original `numpy.copy
389+
<https://docs.scipy.org/doc/numpy/reference/generated/numpy.copy.html>`_ in
390+
the following aspects:
391+
392+
- Input type does not support Python native iterables(list, tuple, ...).
393+
- ``out`` param: cannot perform auto broadcasting. ``out`` ndarray's shape must be the same as the expected output.
394+
- ``out`` param: cannot perform auto type cast. ``out`` ndarray's dtype must be the same as the expected output.
395+
- Does not support "order" parameter.
396+
397+
Examples
398+
--------
399+
Create an array x, with a reference y and a copy z:
400+
401+
>>> x = np.array([1, 2, 3])
402+
>>> y = x
403+
>>> z = np.copy(x)
404+
405+
Note that, when ``x`` is modified, ``y`` is also modified, but not ``z``:
406+
407+
>>> x[0] = 10
408+
>>> x[0] == y[0]
409+
array([1.])
410+
>>> x[0] == z[0]
411+
array([0.])
412+
"""
413+
pass
414+
415+
416+
def _np_reshape(a, newshape, order='C', out=None):
417+
"""
418+
reshape(a, newshape, order='C')
419+
420+
Gives a new shape to an array without changing its data.
421+
This function always returns a copy of the input array if
422+
``out`` is not provided.
423+
424+
Parameters
425+
----------
426+
a : ndarray
427+
Array to be reshaped.
428+
newshape : int or tuple of ints
429+
The new shape should be compatible with the original shape. If
430+
an integer, then the result will be a 1-D array of that length.
431+
One shape dimension can be -1. In this case, the value is
432+
inferred from the length of the array and remaining dimensions.
433+
order : {'C'}, optional
434+
Read the elements of `a` using this index order, and place the
435+
elements into the reshaped array using this index order. 'C'
436+
means to read / write the elements using C-like index order,
437+
with the last axis index changing fastest, back to the first
438+
axis index changing slowest. Other order types such as 'F'/'A'
439+
may be added in the future.
440+
441+
Returns
442+
-------
443+
reshaped_array : ndarray
444+
It will be always a copy of the original array. This behavior is different
445+
from the official NumPy ``reshape`` operator where views of the original array may be
446+
generated.
447+
448+
See Also
449+
--------
450+
ndarray.reshape : Equivalent method.
451+
"""
452+
pass

python/mxnet/gluon/loss.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,15 @@ def __init__(self, weight=None, batch_axis=0, **kwargs):
189189

190190
def hybrid_forward(self, F, pred, label, sample_weight=None):
191191
label = _reshape_like(F, label, pred)
192-
loss = F.abs(label - pred)
192+
loss = F.np.abs(label - pred) if is_np_array() else F.abs(label - pred)
193193
loss = _apply_weighting(F, loss, self._weight, sample_weight)
194-
return F.mean(loss, axis=self._batch_axis, exclude=True)
194+
if is_np_array():
195+
if F is ndarray:
196+
return F.np.mean(loss, axis=tuple(range(1, loss.ndim)))
197+
else:
198+
return F.npx.batch_flatten(loss).mean(axis=1)
199+
else:
200+
return F.mean(loss, axis=self._batch_axis, exclude=True)
195201

196202

197203
class SigmoidBinaryCrossEntropyLoss(Loss):

0 commit comments

Comments
 (0)