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

Feywell: <p>I create a class that inherited from nn.Module. And  I used  a existing function <strong>nms</strong>.<br>
It used index_select().<br>
But I encounter this error:</p>
<blockquote>
<hr>
<p>RuntimeError                              Traceback (most recent call last)<br>
~/experiment/ssd.pytorch-master/train_association_lstm.py in ()<br>
259<br>
260 if <strong>name</strong> == ‘<strong>main</strong>’:<br>
 → 261     train()</p>
<p>~/experiment/ssd.pytorch-master/train_association_lstm.py in train()<br>
179         # print(images.shape)<br>
180         # print(targets)<br>
 → 181         out = net(images,targets)<br>
182         # backprop<br>
183         optimizer.zero_grad()</p>
<p>~/anaconda2/envs/gluon/lib/python3.5/site-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input, **kwargs)<br>
489             result = self._slow_forward(*input, **kwargs)<br>
490         else:<br>
 → 491             result = self.forward(*input, **kwargs)<br>
492         for hook in self._forward_hooks.values():<br>
493             hook_result = hook(self, input, result)</p>
<p>~/experiment/ssd.pytorch-master/association_lstm.py in forward(self, x, targets)<br>
113                 conf.view(conf.size(0), -1, self.num_classes),<br>
114                 self.priors.type(type(x.data)),                 # default boxes<br>
 → 115                 targets<br>
116             )<br>
117         else:</p>
<p>~/anaconda2/envs/gluon/lib/python3.5/site-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input, **kwargs)<br>
489             result = self._slow_forward(*input, **kwargs)<br>
490         else:<br>
 → 491             result = self.forward(*input, **kwargs)<br>
492         for hook in self._forward_hooks.values():<br>
493             hook_result = hook(self, input, result)</p>
<p>~/experiment/ssd.pytorch-master/layers/functions/detection.py in forward(self, loc_data, conf_data, prior_data, targets)<br>
155<br>
156                 # idx of highest scoring and non-overlapping boxes per class<br>
 → 157                 ids, count = nms(boxes, scores, self.nms_thresh, self.top_k)<br>
158                 output.append(<br>
159                     torch.cat((scores[ids[:count]].unsqueeze(1),</p>
<p>~/experiment/ssd.pytorch-master/layers/box_utils.py in nms(boxes, scores, overlap, top_k)<br>
223         idx = Variable(idx)  # add for autograde this version<br>
224         # load bboxes of next highest vals<br>
 → 225         torch.index_select(x1, 0, idx, out=xx1)<br>
226         torch.index_select(y1, 0, idx, out=yy1)<br>
227         torch.index_select(x2, 0, idx, out=xx2)</p>
<p><strong>RuntimeError: index_select(): functions with out=… arguments don’t support automatic differentiation, but one of the arguments requires grad.</strong></p>
</blockquote>
<p>The <strong>nms function</strong> i used <a href="https://github.com/amdegroot/ssd.pytorch/blob/master/layers/box_utils.py#L175" rel="noopener nofollow ugc">here</a><br>
.<br>
Why there is a error here?<br>
How can I fix it?</p>

albanD: <p>Hi,</p>
<p>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 <code>requires_grad=True</code>. If this is in your test code, you should set <code>with torch.no_grad():</code> 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.</p>

Feywell: <p>Thank you! It’s weird. I just change code like :</p>
<blockquote>
<pre><code>    #  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)
</code></pre>
</blockquote>
<p>It is working!</p>

tianrenheyi09: <p>I met the same question as you do ,but I solved this problem by this way</p>
<pre><code class="lang-auto">ids, count = nms(boxes.detach(), scores.detach(), nms_thresh, top_k)
</code></pre>
<p>in this way ,you can use it in training and testing without revising the nms function</p>

Himani_Gulati: <p>I am getting this error with training set, what should i do?</p>

albanD: <p>Hi,</p>
<p>You will have to rewrite your function such that it does not use functions with <code>out=</code>.</p>