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

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Alicia1529 committed Nov 12, 2019
1 parent 65a0048 commit 1855f98
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 35 deletions.
11 changes: 6 additions & 5 deletions python/mxnet/ndarray/numpy/_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -5212,10 +5212,10 @@ def resize(a, new_shape):


@set_module('mxnet.ndarray.numpy')
def full_like(a, fill_value=0, dtype=None, order='K', subok=True, shape=None):
def full_like(a, fill_value=0, dtype=None, order='K', subok=True, shape=None): # pylint: disable=too-many-arguments
"""
Return a full array with the same shape and type as a given array.
Parameters
----------
a : ndarray
Expand All @@ -5239,19 +5239,20 @@ def full_like(a, fill_value=0, dtype=None, order='K', subok=True, shape=None):
Overrides the shape of the result. If order='K' and the number of
dimensions is unchanged, will try to keep order, otherwise,
order='C' is implied.
(Not support at this moment)
Returns
-------
out : ndarray
Array of `fill_value` with the same shape and type as `a`.
See Also
--------
empty_like : Return an empty array with shape and type of input.
ones_like : Return an array of ones with shape and type of input.
zeros_like : Return an array of zeros with shape and type of input.
full : Return a new array of given shape filled with value.
Examples
--------
>>> x = np.arange(6, dtype=int)
Expand Down
10 changes: 5 additions & 5 deletions python/mxnet/numpy/multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -7205,10 +7205,10 @@ def resize(a, new_shape):


@set_module('mxnet.numpy')
def full_like(a, fill_value=0, dtype=None, order='K', subok=True, shape=None):
def full_like(a, fill_value=0, dtype=None, order='K', subok=True, shape=None): # pylint: disable=too-many-arguments
"""
Return a full array with the same shape and type as a given array.
Parameters
----------
a : ndarray
Expand All @@ -7231,19 +7231,19 @@ def full_like(a, fill_value=0, dtype=None, order='K', subok=True, shape=None):
Overrides the shape of the result. If order='K' and the number of
dimensions is unchanged, will try to keep order, otherwise,
order='C' is implied.
Returns
-------
out : ndarray
Array of `fill_value` with the same shape and type as `a`.
See Also
--------
empty_like : Return an empty array with shape and type of input.
ones_like : Return an array of ones with shape and type of input.
zeros_like : Return an array of zeros with shape and type of input.
full : Return a new array of given shape filled with value.
Examples
--------
>>> x = np.arange(6, dtype=int)
Expand Down
13 changes: 4 additions & 9 deletions python/mxnet/numpy_op_fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,13 @@ def __init__(self, fill_value, dtype, order, subok, shape):
super(FullLike, self).__init__()
self._fill_value = fill_value
self._dtype = dtype
self._order = order
self._order = order
self._subok = subok
self._shape = shape

def forward(self, is_train, req, in_data, out_data, aux):
np_version = np.version.version
if np_version == '1.17.0':
out = np.full_like(in_data[0].asnumpy(), self._fill_value, dtype=self._dtype, order=self._order,
subok=self._subok, shape=self._shape)
else:
out = np.full_like(in_data[0].asnumpy(), self._fill_value, dtype=self._dtype, order=self._order,
subok=self._subok)
out = np.full_like(in_data[0].asnumpy(), self._fill_value, dtype=self._dtype, order=self._order,
subok=self._subok)
self.assign(out_data[0], req[0], _mx_np.array(out, dtype=out.dtype, ctx=out_data[0].ctx))

def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
Expand Down Expand Up @@ -106,7 +101,7 @@ def infer_type(self, in_type):
if self._dtype is None:
return (in_type[0],), (in_type[0],), ()
else:
out_dtype = eval('np.'+self._dtype)
out_dtype = eval('np.'+self._dtype) # pylint: disable=W0123
return (in_type[0],), (out_dtype,), ()

def infer_shape(self, in_shape):
Expand Down
8 changes: 4 additions & 4 deletions python/mxnet/symbol/numpy/_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -4825,10 +4825,10 @@ def resize(a, new_shape):


@set_module('mxnet.symbol.numpy')
def full_like(a, fill_value=0, dtype=None, order='K', subok=True, shape=None):
def full_like(a, fill_value=0, dtype=None, order='K', subok=True, shape=None): # pylint: disable=too-many-arguments
"""
Return a full array with the same shape and type as a given array.
Parameters
----------
a : _Symbol
Expand All @@ -4851,12 +4851,12 @@ def full_like(a, fill_value=0, dtype=None, order='K', subok=True, shape=None):
Overrides the shape of the result. If order='K' and the number of
dimensions is unchanged, will try to keep order, otherwise,
order='C' is implied.
Returns
-------
out : _Symbol
Array of `fill_value` with the same shape and type as `a`.
See Also
--------
empty_like : Return an empty array with shape and type of input.
Expand Down
30 changes: 18 additions & 12 deletions tests/python/unittest/test_numpy_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

# pylint: skip-file
from __future__ import absolute_import
from distutils.version import StrictVersion
import sys
import unittest
import itertools
Expand Down Expand Up @@ -4477,9 +4478,16 @@ def __init__(self, fill_value, dtype, order, subok, shape):

def hybrid_forward(self, F, x, *args, **kwargs):
return F.np.full_like(x, self._fill_value, self._dtype, self._order, self._subok, self._shape)

dtypes = [None, 'float16', 'float32', np.int8, np.uint8, np.int32, np.int64,
np.float16, np.float32, np.float64]
def check_python2():
if StrictVersion(platform.python_version()) < platform.python_version('3.0.0'):
return True
return False
def check_int(dtype1, dtype2):
if 'int' in dtype1 or 'int' in dtype2:
return True
return False

dtypes = ['float64', 'float32', 'float16', 'int64', 'int32', 'int8']
shapes = [
(),
(1,),
Expand All @@ -4498,22 +4506,20 @@ def hybrid_forward(self, F, x, *args, **kwargs):
_np_version = _np.version.version
for fill_value, dtype, shape, hybridize, order, subok in itertools.product(
fill_values, dtypes, shapes, flags, orders, subok_list):
param_dtype= _np.random.choice(dtypes)
if check_python2 and check_int(dtype, param_dtype):
continue
a = np.random.uniform(low=0, high=100, size=shape, dtype='float64').astype(dtype)
dtype = _np.random.choice(dtypes)
test = TestFullLike(fill_value, dtype, order, subok, shape)
if _np_version == '1.17.0':
expected_ret = _np.full_like(a, fill_value=fill_value, dtype=dtype, order=order,
subok=subok, shape=shape)
else:
expected_ret = _np.full_like(a, fill_value=fill_value, dtype=dtype, order=order,
subok=subok)
test = TestFullLike(fill_value, param_dtype, order, subok, shape)
expected_ret = _np.full_like(a, fill_value=fill_value, dtype=param_dtype, order=order,
subok=subok)
if hybridize:
test.hybridize()
ret = test(a)
assert_almost_equal(ret.asnumpy(), expected_ret, rtol=1e-3, atol=1e-5)

# check imperative again
ret = np.full_like(a, fill_value, dtype, order, subok, shape)
ret = np.full_like(a, fill_value, param_dtype, order, subok, shape)
assert_almost_equal(ret.asnumpy(), expected_ret, rtol=1e-3, atol=1e-5)


Expand Down

0 comments on commit 1855f98

Please sign in to comment.