I want to reshape a Tensor by multiplying the shape of first two dimensions.
For example,
1st_tensor: torch.Size([12, 10])
to torch.Size([120])
2nd_tensor: torch.Size([12, 10, 5, 4])
to torch.Size([120, 5, 4])
I.e. The first two dimensions shall be merged into one, while the other dimensions shall remain the same.
Is there a smarter way than
1st_tensor.reshape(-1,)
2nd_tensor.reshape(-1,5,4)
,
that can adapt to the shape of different Tensors?
Test cases:
import torch
tests = [
torch.rand(11, 11),
torch.rand(12, 15351, 6, 4),
torch.rand(13, 65000, 8)
]
t = t.reshape(-1, *t.shape[2:])
– iacob