Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have a tensor of shape [5, 2, 18, 4096]. I want to stack the 0th dimension along the 2nd dimension. How can I do it?

The shape of the tensor is [5, 2, 18, 4096]. I want to take each tensor along 0th dimension of size [2, 18, 4096] and stack it on top of another tensor which is of shape from the same tensor [2, 18, 4096] and do it for all tensors along the 0th dimension. The final tensor should be [2, 90, 4096].

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It turns out there’s a very simple approach: torch.hstack always stacks along the second dimension (i.e. along axis 1). For instance, consider the following:

start = torch.arange(120).reshape([5,4,3,2])
result = torch.hstack(list(start))

The tensor start has shape 5,4,3,2, but result has shape (4,15,2), which comes from stacking 5 (4,3,2) arrays along axis 1.

Applying list to a multidimensional tensor breaks the tensor up along the main axis. In this case, list(start) is a list containing 5 (4,3,2)-shaped tensors.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading