Read mat files and convert to tensor

Hi, I want to read mat files in jupyter and convert as tensor. So I use scipy.io but I can’t convert to tensor.

import scipy.io as spio
import torch
import numpy as np
data = spio.loadmat(‘train.mat’)
np_data = np.array(data)
tensor_data = torch.Tensor(np_data)

but result is


TypeError Traceback (most recent call last)
in
----> 1 tensor_data = torch.Tensor(np_data)

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

what should I have to do?

What is currently stored in np_data?
It looks like it’s stored as a numpy.object_, which happens if not all elements have the same dtype.
Dummy example:

x = np.array([1.0, np.sum])
print(x)
> array([1.0, <function sum at 0x7fb77bcdbd08>], dtype=object)
1 Like