Problem with torch.index_select

Hello.

I have a problem running below code

import torch
import torch.nn as nn
from torch.autograd import Variable

weight_list = nn.Parameter(torch.randn(5, 3, 3), requires_grad=True)
indices = torch.LongTensor([0, 2])
weight_select = weight_list.index_select(0, indices)

Therefore, I want to select two 3x3 convolution kernels among the 5.

However, when I execute this code, I get a runtime error:

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    weight_select = weight_list.index_select(0, indices)
  File "/home/sanghyun/anaconda3/lib/python3.6/site-packages/torch/autograd/variable.py", line 681, in index_select
    return IndexSelect.apply(self, dim, index)
RuntimeError: save_for_backward can only save input or output tensors, but argument 0 doesn't satisfy this condition

The strange thing is that if I change requires_grad=True to False, this does not happen.

What is problem?

Thank you.

Weird error message. Fortunately, this doesn’t happen on master branch :slight_smile:

However, you should wrap your indices in Variable.

2 Likes

You are right.

After wrapping indices with Variable, all works fine :smiley:

Thank you!