Shorthand way to convert an ndarray of 2D torch tensors to completely torch tensor

I have some data of the following form,

shape = (batch_size X max_seq_len X embedding_dim)

np.ndarray([torch.tensor([torch.tensor(), torch.tensor(), ....]), ...])

Is there a convenient way to convert it to,

torch.tensor([torch.tensor([torch.tensor(), torch.tensor(), ....]), ...])

Right now, I do


and get the error

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.

Tensors fundamentally cannot hold arbitrary objects, but assuming you have a 1d array of 2d tensors, you can use stack to get a 3d Tensor.

Best regards


Thanks for the reply! Could you elaborate a little more? I’m sort of new to the framework. Thanks.

Well, so ideally you would have shown more about how your data looks like (I must admit I don’t understand that exactly), but say it is similar to

a,b,c = torch.randn(3, 2, 10) # three 2x10 tensors
arr = numpy.array([a,b,c], dtype=object) # array of size 3 of 2x10 tensors

then you can do

t = torch.stack(list(arr), dim=0) # 3x2x10 tensor

Best regards