diff --git a/amalgamation/python/mxnet_predict.py b/amalgamation/python/mxnet_predict.py index 091bfbb1cf14..a91d3849b0d2 100644 --- a/amalgamation/python/mxnet_predict.py +++ b/amalgamation/python/mxnet_predict.py @@ -163,7 +163,7 @@ def forward(self, **kwargs): for k, v in kwargs.items(): if not isinstance(v, np.ndarray): raise ValueError("Expect numpy ndarray as input") - v = np.ascontiguousarray(v, dtype=np.float32) + v = np.asarray(v, dtype=np.float32, order='C') _check_call(_LIB.MXPredSetInput( self.handle, c_str(k), v.ctypes.data_as(mx_float_p), diff --git a/python/mxnet/ndarray/ndarray.py b/python/mxnet/ndarray/ndarray.py index de2ad692adfc..60b3834054db 100644 --- a/python/mxnet/ndarray/ndarray.py +++ b/python/mxnet/ndarray/ndarray.py @@ -866,7 +866,7 @@ def _sync_copyfrom(self, source_array): except: raise TypeError('array must consist of array-like data,' + 'type %s is not supported' % str(type(array))) - source_array = np.ascontiguousarray(source_array, dtype=self.dtype) + source_array = np.asarray(source_array, dtype=self.dtype, order='C') if source_array.shape != self.shape: raise ValueError('Shape inconsistent: expected %s vs got %s'%( str(self.shape), str(source_array.shape))) diff --git a/python/mxnet/test_utils.py b/python/mxnet/test_utils.py index 49a1a0f10277..c555b2fdfaf8 100644 --- a/python/mxnet/test_utils.py +++ b/python/mxnet/test_utils.py @@ -753,7 +753,7 @@ def as_stype(var, stype, dtype): if stype == 'default': executor.arg_dict[k][:] = as_stype(v, stype, dtype=dtype) for k in location: - location[k] = np.ascontiguousarray(location[k]) + location[k] = np.asarray(location[k], order='C') for k, v in location.items(): if v.dtype.kind != 'f': continue diff --git a/tests/python/unittest/test_ndarray.py b/tests/python/unittest/test_ndarray.py index a1c178f8234e..14782e22a3c6 100644 --- a/tests/python/unittest/test_ndarray.py +++ b/tests/python/unittest/test_ndarray.py @@ -118,6 +118,14 @@ def test_ndarray_setitem(): x_np[:, -3:-1, -2:-1] = 1 assert same(x.asnumpy(), x_np) + # numpy assignment for empty axis + for trivial_shape in [(), (1), (1, 1), (1, 1, 1)]: + x = mx.nd.zeros(trivial_shape) + x[:] = np.ones(trivial_shape) + x_np = np.ones(trivial_shape, dtype=x.dtype) + assert x.shape == trivial_shape + assert same(x.asnumpy(), x_np) + @with_seed() def test_ndarray_elementwise(): @@ -217,6 +225,13 @@ def test_ndarray_onehot(): assert same(npy, arr.asnumpy()) +def test_init_from_scalar(): + npy = np.ones([]) + arr = mx.nd.array(npy) + assert arr.shape == () + assert same(npy, arr.asnumpy()) + + @with_seed() def test_ndarray_copy(): c = mx.nd.array(np.random.uniform(-10, 10, (10, 10)))