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

index a list of tensors

I have a list object named " imgs " of tensors (50 images).
I have an array of indices (indi) of length 29.

how do I index the list of tensors with the array of indices?

when I do the following I get:

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

imgs[indi] 

TypeError: only integer scalar arrays can be converted to a scalar index

Thanks

>Solution :

Assuming these are normal python lists then you can use a list comprehension

result = [imgs[i] for i in indi]

which will give a list of tensors.

If you further want to make this a single tensor containing the images you can use torch.stack

result = torch.stack([imgs[i] for i in indi], dim=0)
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