How to make a Tensor from ndarray of type object

I am new to PyTorch. I have an array of length 6 and shape (6, ) when I run torch.from_numpy(data_array), I got this error:

TypeError: can’t convert np.ndarray of type numpy.object_. The only supported types are: float64, float32, float16, complex64, complex128, int64, int32, int16, int8, uint8, and bool.

I have also tried with pd.DataFrame, but face another error:

TypeError: expected np.ndarray (got DataFrame)

data_array = np.load('Dance0368.npy', allow_pickle=True)
data = pd.DataFrame(data_array)
len(data_array)
6

data_array.shape
(6,)

torch.from_numpy(data_array)
TypeError: can't convert np.ndarray of type numpy.object_. The only supported types are: float64, float32, float16, complex64, complex128, int64, int32, int16, int8, uint8, and bool.

data.shape
(6, 1)

data.dtypes
0    object
dtype: object

data
	0
0	{'cam': [0.371, -0.4058, 0.0315], 'pose': [-2....
1	{'cam': [0.3848, 0.198, 0.03775], 'pose': [-3....
2	{'cam': [0.5635, 0.684, 0.1471], 'pose': [-3.0...
3	{'cam': [0.3994, 0.4841, 0.01412], 'pose': [-3...
4	{'cam': [0.3486, -0.0975, 0.03378], 'pose': [3...
5	{'cam': [0.401, -0.661, -0.0003161], 'pose': [...

torch.from_numpy(data)
TypeError: expected np.ndarray (got DataFrame)

X = torch.from_numpy(data.values)
TypeError: can't convert np.ndarray of type numpy.object_. The only supported types are: float64, float32, float16, complex64, complex128, int64, int32, int16, int8, uint8, and bool.

torch.Tensor(data)
ValueError: expected sequence of length 6 at dim 0 (got 1)

Hi Muhammad,

The problem is caused by the data type you are trying to convert into a torch.Tensor, PyTorch doesn’t support complex data structures such as dictionaries.

You can convert your data into PyTorch tensors by:

data_array = np.array({"cam":[0,1,1], "pos":[0.5,1.3,1.9]})
cam, pos = torch.tensor([data_array.item()["cam"], data_array.item()["pos"]])

or

tensorData= torch.tensor([data_array.item()["cam"], data_array.item()["pos"]])

Convert the data to list instead of ndnumpy