I’m a bit confused about the mentioned shapes.



 ck172:

and I have a length tensor [5,3,2],
if I directly flatten the tensor on dimension 2, the shape will be [1,3,15]


This shouldn’t be possible via reshaping the tensor as the former one has 5*3*2=30 elements while the latter has 1*3*15=45.



 ck172:

How to flatten the tensor based on the length of dimension 3 so that the result can be [1,3,10] where 10 = 5+3+2?


“Flattening” dimensions would be a product of their shapes.
If you want to create a tensor in the shape [1, 3, 10] by flattening dim0 with dim2 you could use:
x = torch.randn(5, 3, 2)
y = x.permute(1, 0, 2).contiguous().view(1, 3, -1)
print(y.shape)
> torch.Size([1, 3, 10])