How do I use Pytorch's View function correctly?
I have a tensor of size x={4,2,C,H,W}. I need it reshape to y={8,C,H,W}, but I want to make sure the images are stored in the right order so say an image at x[1,0,:,:,:] has to be equal to y[2,C,H,W]. I know I can use the view function for this but I am not sure how to use it correctly.

Currently I am doing it as such

feat_imgs_all = feat_imgs_all.view(
    rgb.shape[0], rgb.shape[1], feat_imgs_all.shape[1], 
    feat_imgs_all.shape[2], feat_imgs_all.shape[3])


This seems really hacky, is there a way I can just feed the first two shapes, and pytorch figures out the rest?

You can do it easily using flatten and end_dim argument, see documentation:

import torch

a = torch.randn(4, 2, 32, 64, 64)
flattened = a.flatten(end_dim=1)

torch.all(flattened[2, ...] == a[1, 0, ...]) # True


view could be used as well, like below, though it's not too readable nor too pleasant:

import torch

a = torch.randn(4, 2, 32, 64, 64)
flattened = a.view(-1, *a.shape[2:])

torch.all(flattened[2, ...] == a[1, 0, ...]) # True as well

