Multidimensional List of Tensors to Multidimensional Tensor

I’m looking for a way to transform a multidimensional list of tensors that I assemble during one forward pass into a single tensor for further computations.

In three loops (over the direction (D), height (H) and width (W) of an image) I compute hidden state tensors of shape (Batch (B) x Hidden Units (H)), add them to a list, and at the end of this loop add this list to another list in the outside loop. In the end I have a list of shape (D x H x W) containing the (B x H) tensors.

How do I efficiently convert this to one tensor of shape (D x H x W x B x H) so I can apply the next weight matrix to it? Or is there a better way to collect the states in the first place? I thought about just concatenating the states together with torch.cat, but then I would need one initial tensor and always append to different dimensions.

(Also if I’m at it, what would be the way to combine the first and the last dimension of the result? Say I have 4 directions and hidden size of 16, how do I get (H x W x B x 64) from (4 x H x W x B x 16)? Just torch.view?)