Hi, Pytorch beginner here.
Does PyTorch supports backward through topk operation on Variables?
    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)

upon loss.backward(), I get mask.grad as
    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)]

But
for f in net.parameters():
        print f.grad

gives,
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

for all parameters.
Am I doing something wrong?
Is there a better way to do this?
Thanks in advance.