Backward through topk operation on Variable

Suriya: <p>Hi, Pytorch beginner here.</p>
<p>Does PyTorch supports backward through topk operation on Variables?</p>
<pre><code>    features = get_feature(x)          ### x is (B, 3, 128, 128)
    
    score = regressor(features)        ### score here is (B, 1, 128, 128)
    size_ = score.size()
    score = score.view(-1)

    topk, indices = torch.topk(score, score.size()[0]/2)

    mask = Variable(torch.zeros(score.size()).cuda(), requires_grad=True)

    mask = mask.scatter(0, indices, 1)          ### thresholding
    
    mask = mask.view(size_)            ### mask here is (B, 1, 128, 128)
</code></pre>
<p>upon loss.backward(), I get mask.grad as</p>
<pre><code>    Variable containing:
    ( 0 , 0 ,.,.) = 
      1.4381e-07  4.3006e-07  1.3524e-08  ...   9.8986e-08  1.8383e-07  3.0903e-07
      4.8662e-07  6.9170e-07  3.4237e-08  ...  -1.7501e-08  1.2937e-08  1.4631e-07
      4.7671e-07  3.3662e-07  4.4110e-08  ...  -2.6617e-08  6.9062e-08  1.2557e-07
                     ...                   ⋱ 
      [torch.cuda.FloatTensor of size 64x1x128x128 (GPU 0)]
</code></pre>
<p>But</p>
<pre><code>for f in net.parameters():
        print f.grad
</code></pre>
<p>gives,</p>
<pre><code>Variable containing:
(0 ,0 ,.,.) = 
   0   0   0  ...    0   0   0
   0   0   0  ...    0   0   0
   0   0   0  ...    0   0   0
     ...       ⋱       ...    
   0   0   0  ...    0   0   0
   0   0   0  ...    0   0   0
   0   0   0  ...    0   0   0
</code></pre>
<p>for all parameters.</p>
<p>Am I doing something wrong?<br>
Is there a better way to do this?<br>
Thanks in advance.</p>

SimonW: <p>PyTorch definitely is able to backward through topk. Could you please post a full script with how loss is calculated and how the topk variable is used? Thanks <img src="//discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

Suriya: <p>Here is the script. net_A is encoder-decoder networks, its forward() computes a binary mask (code in question) and net_B is autoencoder trained for reconstruction.</p>
<pre><code>net_A.train()
net_B.eval()
optimizer_A.zero_grad()
for batch_idx, inputs in enumerate(train_loader):
    
    if use_cuda:
        inputs = inputs.cuda()
    
    inputs_a = Variable(inputs)
    
    mask = net_A(inputs_a)                       ### takes image as input and outputs mask of same resolution
    mask.retain_grad()
    inputs_b = torch.mul(inputs_a, mask)
    outputs_b = net_B(inputs_b)                ### takes corrupted image as input and outputs reconstruction of same resolution
    
    targets_b = inputs_a.clone()
    targets_b[:,0,:,:] = torch.mul(targets_b[:,0,:,:], 1.0/(3.0*std_bgr[0]))
    targets_b[:,1,:,:] = torch.mul(targets_b[:,1,:,:], 1.0/(3.0*std_bgr[1]))
    targets_b[:,2,:,:] = torch.mul(targets_b[:,2,:,:], 1.0/(3.0*std_bgr[2]))

    loss = 1 - torch.sum((outputs_b*(1-mask) - targets_b*(1-mask))**2)/outputs_b.nelement()
    
    for f in net_A.parameters():
        f.retain_grad()

    loss.backward()
    
    print mask.grad                          ### gives gradient values
    for f in net_A.parameters():
        print f.grad                         ### gives zeros for all params

    optimizer_A.step()
    optimizer_A.zero_grad()




class net_A(nn.Module):
   def __init__(self, block, layers):
        super(net_A, self).__init__()
        #### Resnet 
        self.inplanes = 64
        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,  bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
        #### Resnet 

        #### Deconvs
        self.deconv1 = nn.ConvTranspose2d(512*block.expansion, 512, kernel_size=4, stride=2, padding=1, bias=False)
        self.bn_d1 = nn.BatchNorm2d(512)
        self.deconv2 = nn.ConvTranspose2d(512, 256, kernel_size=4, stride=2, padding=1, bias=False)
        self.bn_d2 = nn.BatchNorm2d(256)
        self.deconv3 = nn.ConvTranspose2d(256, 128, kernel_size=4, stride=2, padding=1, bias=False)
        self.bn_d3 = nn.BatchNorm2d(128)
        self.deconv4 = nn.ConvTranspose2d(128, 64, kernel_size=4, stride=2, padding=1, bias=False)
        self.bn_d4 = nn.BatchNorm2d(64)
        self.deconv5 = nn.ConvTranspose2d(64, 32, kernel_size=4, stride=2, padding=1, bias=False)
        self.bn_d5 = nn.BatchNorm2d(32)
        #### Deconvs

        self.regressor = nn.Conv2d(32, 1, kernel_size=1, stride=1, padding=0, bias=True)



        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

   def _make_layer(self, block, planes, blocks, stride=1):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes, planes * block.expansion,
                          kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(planes * block.expansion),        )

       layers = []
       layers.append(block(self.inplanes, planes, stride, downsample))
       self.inplanes = planes * block.expansion
       for i in range(1, blocks):
           layers.append(block(self.inplanes, planes))

       return nn.Sequential(*layers)

   def get_feature(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.deconv1(x)
        x = self.bn_d1(x)
        x = self.relu(x)
    
        x = self.deconv2(x)
        x = self.bn_d2(x)
        x = self.relu(x)
    
        x = self.deconv3(x)
        x = self.bn_d3(x)
        x = self.relu(x)
    
        x = self.deconv4(x)
        x = self.bn_d4(x)
        x = self.relu(x)
    
        x = self.deconv5(x)
        x = self.bn_d5(x)
        x = self.relu(x)

        return x

    
   def forward(self, x):
        features = self.get_feature(x)          ### x is (B, 3, 128, 128)

        score = self.regressor(features)        ### score here is (B, 1, 128, 128)
        size_ = score.size()
        score = score.view(-1)

        topk, indices = torch.topk(score, score.size()[0]/2)

        mask = Variable(torch.zeros(score.size()).cuda(), requires_grad=True)

        mask = mask.scatter(0, indices, 1)          ### thresholding

        mask = mask.view(size_)            ### mask here is (B, 1, 128, 128)

        return mask</code></pre>

Sourabh_Daptardar: <aside class="quote no-group" data-username="Suriya" data-post="3" data-topic="9197">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/ce73a5/48.png" class="avatar"> Suriya:</div>
<blockquote>
<p>topk, indices = torch.topk(score, score.size()[0]/2)</p>
</blockquote>
</aside>
<p>I believe <strong>‘topk’</strong> is <strong>differentiable</strong> and <strong>‘indices’</strong> is <strong>not</strong> (in the autograd graph sense).</p>
<p>Digging in autograd code I found the following line related to class Topk(_MultiSelectionFunction):</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/pytorch/blob/4f33b136d8ba547725022840ee8adf77e1ed245e/torch/autograd/_functions/tensor.py#L537">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/blob/4f33b136d8ba547725022840ee8adf77e1ed245e/torch/autograd/_functions/tensor.py#L537" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/4f33b136d8ba547725022840ee8adf77e1ed245e/torch/autograd/_functions/tensor.py#L537" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/4f33b136d8ba547725022840ee8adf77e1ed245e/torch/autograd/_functions/tensor.py#L537</a></h4>



    <pre class="onebox"><code class="lang-py">
      <ol class="start lines" start="527" style="counter-reset: li-counter 526 ;">
          <li></li>
          <li>@staticmethod</li>
          <li>def forward(ctx, input, dim, return_indices, args):</li>
          <li>    fn = getattr(input, ctx._forward_cls.__name__.lower())</li>
          <li>    ctx.return_indices = return_indices</li>
          <li>    ctx.input_size = input.size()</li>
          <li>    ctx.dim = dim</li>
          <li>    output, indices = fn(*args)</li>
          <li>    if return_indices:</li>
          <li>        ctx.save_for_backward(indices)</li>
          <li class="selected">        ctx.mark_non_differentiable(indices)</li>
          <li>        return output, indices</li>
          <li>    else:</li>
          <li>        ctx.indices = indices</li>
          <li>        return output</li>
          <li></li>
          <li>@staticmethod</li>
          <li>def backward(ctx, grad_output, grad_indices=None):</li>
          <li>    grad_input = Variable(grad_output.data.new(ctx.input_size).zero_())</li>
          <li>    if ctx.return_indices:</li>
          <li>        indices, = ctx.saved_variables</li>
      </ol>
    </code></pre>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>Perhaps, <a class="mention" href="/u/apaszke">@apaszke</a>  or any one familiar with autograd code, could clarify and confirm. Is there some documentation on this ?</p>

SimonW: <p>The values are differentiable, but the indices aren’t, similar to how argmax is just not a differentiable function.</p>

pinocchio: <p><a class="mention" href="/u/simonw">@SimonW</a>  Can you clarify how <code>topk</code> is differentiable? How is it different from a generic k-argmax?</p>

SimonW: <p>As I said above, the values are, the indices are not.</p>