• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: extracting tensor values given tensor index values torch

Resolved: extracting tensor values given tensor index values torch

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I have a tensor of values val with shape (b,n) and a tensor of indexes ind with shape (b,m) (where n>m). My goal is to take the values in val that corresponds to the indexes in ind. Ive tried using val[ind], but it only expanded the dimensions of val, rather than taking only the relevant items
the wanted output is

Answer:

You can perform such operation using torch.gather:
Essentially indexing val‘s 2nd dimension using ind‘s values. The returned tensor out follows:

If you have better answer, please add a comment about this, thank you!

indexing python pytorch slice
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Unable to run Docker .NET 6 console image

02/04/2023

Resolved: Input Focus between two React Components

02/04/2023

Resolved: linq2db throws exception when filtering by nested collection

02/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.