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

Split torch tensor dimension on two

If I have a torch tensor of shape [2, 12] is it possible to make from it a tensor of shape [3, 2, 4] in such a way that data will be split on chunks along the last dimension? View function does not change the order of data, but here I need change the order, that the new dimension will be the first, and the second dimension would not be changed

Here is example image of how data must be split

[2, 12]  
............   
............  
   
[3, 2, 4]  
....|....|....   
....|....|....   

torch.split function does exactly what I need in sense of shape, but it outputs not a tensor, but a tuple

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

>Solution :

I’m assuming you want to split the 12 sized dimension into the leading 3 and trailing 4 dimensions. You simply need to view and permute dimensions:

X = torch.permute(X.view(2, 3, 4), (1, 0, 2))
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