5 tensor functions using indices in PyTorch

Original article was published on Deep Learning on Medium

5. index_select(dim, index)

This function is quite different, to the other ones we looked at in this article, but it might also be the most useful. Again dim describes the dimension and index is a tensor, but this time, the tensor we call the function on (in our case x), is not changed, so we have to assign the function call to a new tensor:

So we can see, that x stays the same and y only includes the columns with the indices, we specified in index. So with this function, you can create a new tensor with only some rows or columns of the original tensor.

Another thing you might have noticed, is that, in contrast to every function, index_select does not have a _ after the second word. In PyTorch, the underscore means, that the original element you call the function on is changed and the former value is lost.
For every function we looked at, there is a version without the underscore at the end (but for index_select no version with underscore) which you need to assign to a new variable to keep the tensor, so we actually covered 9 functions!