In PyTorch I have a 5D tensor X
of dimensions B x 9 x C x H x W
. I want to convert it into a 4D tensor Y
with dimensions B x 9C x H x W
such that concatenation happens channel wise.
To illustrate let,
JavaScript
x
6
1
a = X[1,0,:,:,:]
2
b = X[1,1,:,:,:]
3
c = X[1,2,:,:,:]
4
5
i = X[1,8,:,:,:]
6
Then in the tensor Y
, a to i
should be channel wise concatenated.
Advertisement
Answer
Try:
JavaScript
1
6
1
import torch
2
x = torch.rand(3, 4, 3, 2, 6)
3
print(x.shape)
4
y=x.flatten(start_dim=1, end_dim=2)
5
print(y.shape)
6
JavaScript
1
3
1
torch.Size([3, 4, 3, 2, 6])
2
torch.Size([3, 12, 2, 6])
3