在使用Tsensor.from_numpy()算子时,发现相同的数据在pytorch上能成功实现,但在mindspore出现了一些问题,想请教一下哪里出错了?
import torch
import numpy as np
from mindspore import Tensor
x = np.zeros((64, 480, 240, 3))
output1 = torch.from_numpy(x.transpose([3, 0, 1, 2]))
output = Tensor.from_numpy(x.transpose([3, 0, 1, 2]))
print(output1.shape)
print(x.shape)
print(output.shape)
D:\text1\venv\Scripts\python.exe D:/mindspore/2.py
Traceback (most recent call last):
File "D:\mindspore\2.py", line 7, in <module>
output = Tensor.from_numpy(x.transpose([3, 0, 1, 2]))
File "D:\text1\venv\lib\site-packages\mindspore\common\tensor.py", line 431, in from_numpy
return Tensor(Tensor_.from_numpy(array))
RuntimeError: Array should be C contiguous.
----------------------------------------------------
- C++ Call Stack: (For framework developers)
----------------------------------------------------
mindspore\ccsrc\pybind_api\ir\tensor_py.cc:247 MakeTensorOfNumpy
将output = Tensor.from_numpy(x.transpose([3, 0, 1, 2]))改为
output = Tensor.from_numpy(np.ascontiguousarray(x.transpose([3, 0, 1, 2])))试试。