saruftw
(Sarthak)
November 6, 2019, 3:35am
1
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
torch.from_numpy(devX)
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.
tom
(Thomas V)
November 6, 2019, 5:22am
2
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
Thomas
saruftw
(Sarthak)
November 6, 2019, 5:24am
3
Thanks for the reply! Could you elaborate a little more? I’m sort of new to the framework. Thanks.
tom
(Thomas V)
November 6, 2019, 5:31am
4
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
Thomas