Torch.cat() returns 3D tensor rather than 4D tensor

Hello l have a list of tensors Y obtained by concatenating 10 tensors each of dimension (16,32,3) . Hence Y is of dimension [10,16,32,3].

Now l would like to concatenate Y using torch.cat() as follow :

        Y = []
        for O in np.arange(len(x)):
            x_p = self.conv_layer(x[O])
            Y.append(x_p)

Then l apply torch.cat() to Y as follow :


x = torch.cat(Y)

However :

x.size()
torch.Size([160, 32, 3])

Rather than :
torch.Size([10,16, 32, 3])

What is wrong with my code ?

Thank you

Hi,

torch.cat concatenates along an existing dimension. and so the number of dimensions of the output is the same as the inputs.
torch.stack stacks a list of tensors along an new dimension. In that case the number of dimensions of the output is one more than the inputs.
I think you’re looking for the second in your case.

3 Likes