Index_select(): functions with out=... arguments don't support automatic differentiation

I create a class that inherited from nn.Module. And I used a existing function nms.
It used index_select().
But I encounter this error:


RuntimeError Traceback (most recent call last)
~/experiment/ssd.pytorch-master/train_association_lstm.py in ()
259
260 if name == ‘main’:
–> 261 train()

~/experiment/ssd.pytorch-master/train_association_lstm.py in train()
179 # print(images.shape)
180 # print(targets)
–> 181 out = net(images,targets)
182 # backprop
183 optimizer.zero_grad()

~/anaconda2/envs/gluon/lib/python3.5/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
–> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)

~/experiment/ssd.pytorch-master/association_lstm.py in forward(self, x, targets)
113 conf.view(conf.size(0), -1, self.num_classes),
114 self.priors.type(type(x.data)), # default boxes
–> 115 targets
116 )
117 else:

~/anaconda2/envs/gluon/lib/python3.5/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
–> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)

~/experiment/ssd.pytorch-master/layers/functions/detection.py in forward(self, loc_data, conf_data, prior_data, targets)
155
156 # idx of highest scoring and non-overlapping boxes per class
–> 157 ids, count = nms(boxes, scores, self.nms_thresh, self.top_k)
158 output.append(
159 torch.cat((scores[ids[:count]].unsqueeze(1),

~/experiment/ssd.pytorch-master/layers/box_utils.py in nms(boxes, scores, overlap, top_k)
223 idx = Variable(idx) # add for autograde this version
224 # load bboxes of next highest vals
–> 225 torch.index_select(x1, 0, idx, out=xx1)
226 torch.index_select(y1, 0, idx, out=yy1)
227 torch.index_select(x2, 0, idx, out=xx2)

RuntimeError: index_select(): functions with out=… arguments don’t support automatic differentiation, but one of the arguments requires grad.

The nms function i used here
.
Why there is a error here?
How can I fix it?

Hi,

This function appears to be written to be used only at test time (not training). And you appear to call it will a Tensor that requires_grad=True. If this is in your test code, you should set with torch.no_grad(): around your network to specify that you don’t need gradients. If this is training code, then I’m afraid you’ll have to modify the nms function to use only operations that support the autograd.

1 Like

Thank you! It’s weird. I just change code like :

    #  torch.index_select(x1, 0, idx, out=xx1)
    #  torch.index_select(y1, 0, idx, out=yy1)
    #  torch.index_select(x2, 0, idx, out=xx2)
    #  torch.index_select(y2, 0, idx, out=yy2)
    xx1 = torch.index_select(x1, 0, idx)
    yy1 = torch.index_select(y1, 0, idx)
    xx2 = torch.index_select(x2, 0, idx)
    yy2 = torch.index_select(y2, 0, idx)

It is working!

1 Like

I met the same question as you do ,but I solved this problem by this way

ids, count = nms(boxes.detach(), scores.detach(), nms_thresh, top_k)

in this way ,you can use it in training and testing without revising the nms function

I am getting this error with training set, what should i do?

Hi,

You will have to rewrite your function such that it does not use functions with out=.