mindspore-ai / mindspore

MindSpore is a new open source deep learning training/inference framework that could be used for mobile, edge and cloud scenarios.
https://gitee.com/mindspore/mindspore
Apache License 2.0
4.24k stars 701 forks source link

mindspore.numpy doesn't support create array from numpy.ndarray #135

Closed leonwanghui closed 3 years ago

leonwanghui commented 3 years ago

Environment

Hardware Environment(Ascend/GPU/CPU):

/device cpu

Software Environment:

Describe the current behavior

When I want to convert a numpy ndarray to Tensor using the mindspore.numpy module, some errors occurred:

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  
D:\Applications\Anaconda3\lib\site-packages\mindspore\numpy\array_creations.py:157: in asarray
    return asarray_const(a, dtype)
D:\Applications\Anaconda3\lib\site-packages\mindspore\ops\primitive.py:188: in __call__
    return _run_op(self, self.name, args)
D:\Applications\Anaconda3\lib\site-packages\mindspore\common\api.py:75: in wrapper
    results = fn(*arg, **kwargs)
D:\Applications\Anaconda3\lib\site-packages\mindspore\ops\primitive.py:556: in _run_op
    output = real_run_op(obj, op_name, args)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  

obj = array([[ 10.,   4.,   2.,   5.,  18., -10.,  -4.,   3.,   7.]],
      dtype=float32)

    def get_obj_type(obj):
        """Get the obj type."""
        obj_type = RESOLVE_TYPE_INVALID
        if obj is None:
            obj_type = RESOLVE_TYPE_NONE
        elif isinstance(obj, types.FunctionType):
            obj_type = RESOLVE_TYPE_FUNCTION
        elif isinstance(obj, types.MethodType):
            obj_type = RESOLVE_TYPE_METHOD
        elif isinstance(obj, type):
            obj_type = RESOLVE_TYPE_CLASS_TYPE
        elif _is_class_instance(obj):
            obj_type = RESOLVE_TYPE_CLASS_INSTANCE
        else:
            # here for ndarray, just print its shape (in case of the array to large and print many data in screen)
            is_ndarray = type(obj).__name__ == 'ndarray' and hasattr(obj, 'shape')
>           raise TypeError(f'Invalid object with type `{type(obj)}` and {"shape" if is_ndarray else "value"} '
                            f'`{obj.shape if is_ndarray else obj}`.')
E           TypeError: Invalid object with type `<class 'numpy.ndarray'>` and shape `(1, 9)`.

D:\Applications\Anaconda3\lib\site-packages\mindspore\_extends\parse\parser.py:252: TypeError

Same errors occurred when I use numpy.array(input) method.

Describe the expected behavior

Fix the errors.

Steps to reproduce the issue

  1. from mindspore import numpy as np
  2. import numpy as onp
  3. np.asarray(onp.ones((1, 10)))

Related log / screenshot

Special notes for this issue

cxl-ll commented 3 years ago

Hello, I suggest you upgrade python to version 3.7.5 and try again.

yanglf1121 commented 3 years ago

Hi, actually mindspore.numpy.array and mindspore.numpy.asarray cannot accept original numpy array as input, please see the description here: https://www.mindspore.cn/doc/api_python/zh-CN/r1.2/mindspore/numpy/mindspore.numpy.array.html?highlight=array#mindspore.numpy.array

leonwanghui commented 3 years ago

@yanglf1121 Got it, thanks for the elaboration!