RuntimeError: expecting vetor of indices at /home/*torch/lib/THC/generic/THCTensorIndex.cu:405

when i want to do index like this, i meet this error:

indice = torch.cat((x1,y1),1).long()

output = input.index_select(0,indice)

anyone know why? thanks~

The index argument in index_select has to be a 1D tensor. I’m not sure exactly what you’re trying to do here, but it looks like indice is a 2 x 1 tensor (if I’m not mistaken). You can convert it over with indice.squeeze(1)

yeah, the indice is a m*n(m,n>2)tensor since it contains the cooradinate of the value that i want to index.
do you have some advice how to do it? thanks~

What exactly are you trying to do?

If input has size (m, n) and indice has size (m, n) where indice is binary (its elements are 0 or 1), you can do index.masked_select(indice).

@richard thanks for your reply.
my input is a two-demension tensor , and i have a two-demension tensor contain the coordinates of the element that i want to obtain from the input tensor .
can you give me some advice how to obtain the element according to the coordinate?

Ah I see. Try the following:

import torch
input = torch.randn(3, 3)  # the 2d tensor with eleemnts
index = torch.LongTensor([1, 2])  # your hypothetical index
input[index[0], index[1]]  # gets you the element at (1, 2) of input

yeah, but when the index contains coordinates of more than one elements , i alway encounter the indexError.
the details are as follows:
index0 = torch.LongTensor[x])
index1 = torch.LongTensor([y]) #where x,y are one-demension tensor contains many pairs coordinates
input[index0,index1]

i got the error like this:
IndexError: when perfroming advanced indexing the indexing objects must be LongtTensor or convertible to LongTensor.
but as you see, the index’s type is already LongTensor.

I’m not 100% sure I understand what your example is, but I think this is what you want. Given the following:
image
You want to be able to “index” x with i. I think the following code should get you the desired behavior:

torch.cat([x[coord[0], coord[1]] for coord in i])

This iterates through the coordinates listed in i, pulls them out of x, and finally concatenates to make a tensor.

1 Like

@richard yeah ,this is what i want to do ,thanks for your patient reply:grinning: