Question:
I have a tensor of valuesval
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 itemsAnswer:
You can perform such operation usingtorch.gather
: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!