diff --git a/mindspore/python/mindspore/common/tensor.py b/mindspore/python/mindspore/common/tensor.py index 419d56af344..06177551522 100644 --- a/mindspore/python/mindspore/common/tensor.py +++ b/mindspore/python/mindspore/common/tensor.py @@ -434,6 +434,9 @@ class Tensor(Tensor_): >>> print(output) [1 2] """ + if isinstance(array, np.ndarray) and not array.flags['C_CONTIGUOUS']: + array = np.ascontiguousarray(array) + return Tensor(Tensor_.from_numpy(array)) def assign_value(self, value): diff --git a/tests/ut/python/ir/test_tensor.py b/tests/ut/python/ir/test_tensor.py index 110772b8ed2..83bd1d0a5d3 100644 --- a/tests/ut/python/ir/test_tensor.py +++ b/tests/ut/python/ir/test_tensor.py @@ -555,3 +555,7 @@ def test_tensor_from_numpy(): with pytest.raises(TypeError): # incorrect input. t = ms.Tensor.from_numpy([1, 2, 3]) + + x = np.array([[1, 2], [3, 4]], order='F') + b = Tensor.from_numpy(x) + assert np.all(b.asnumpy() == np.array([[1, 2], [3, 4]]))