I’ve two masks tensors which each have dimension of [b,c,w,h] (c=63*63)
weight_1 and weight_2 are their corresponding label [b,1,w,h] ] indicating which place in masks ought to I choose
As a result of mask_1 and mask_2 are paired (mask_1[i] is paired with mask_2[i] ), I need to choose widespread min numbers of masks for every pair
beneath is my code

def select_mask(mask_1, weight_1, mask_2, weight_2, o_sz=63, g_sz=127):
    mask_1 = mask_1.permute(0, 2, 3, 1).contiguous()
    mask_2 = mask_2.permute(0, 2, 3, 1).contiguous()

    sel_pm_1 = torch.tensor([])
    sel_pm_2 = torch.tensor([])
    sel_pm_1 = torch.autograd.Variable(sel_pm_1).cuda()
    sel_pm_2 = torch.autograd.Variable(sel_pm_2).cuda()
    for i in vary(weight_1.dimension(0)):
        tmp = weight_1[i][0].view(-1) #[w*h] (w=25,h=25)
        tmp2 = weight_2[i][0].view(-1) #[w*h]
        tmp = Variable(tmp.knowledge.eq(1).nonzero())  
        tmp2 = Variable(tmp2.knowledge.eq(1).nonzero())
        n_tmp = tmp.nelement()
        n_tmp2 = tmp2.nelement()
        mi = min(n_tmp, n_tmp2)
        tmp = tmp[:mi].squeeze()
        tmp2 = tmp2[:mi].squeeze()

        T = mask_1[i].view(25*25,63*63)
        T2 = mask_2[i].view(25*25,63*63)
        tmp_pm_1 = torch.index_select(T,0,tmp)
        tmp_pm_2 = torch.index_select(T2,0,tmp2)
        sel_pm_1 = torch.cat([tmp_pm_1,sel_pm_1])
        sel_pm_2 = torch.cat([tmp_pm_2,sel_pm_2])

    if sel_pm_1.nelement()==Zero or sel_pm_2.nelement()==0: return None,None,False

    #sel_pm_1 = [n,63*63]
    sel_pm_1 = sel_pm_1.view(-1, 1, o_sz, o_sz)
    sel_pm_1 = nn.UpsamplingBilinear2d(dimension=[g_sz, g_sz])(sel_pm_1)
    sel_pm_1 = sel_pm_1.view(-1, g_sz , g_sz)

    sel_pm_2 = sel_pm_2.view(-1, 1, o_sz, o_sz)
    sel_pm_2 = nn.UpsamplingBilinear2d(dimension=[g_sz, g_sz])(sel_pm_2)
    sel_pm_2 = sel_pm_2.view(-1, g_sz , g_sz)
    return sel_pm_1, sel_pm_2, True

Forwarding appears no downside , nevertheless , I obtained the next error message
The place might probably go improper?

Traceback (most up-to-date name final):
  File "/dwelling/teresa/SiamMask/instruments/train_siammask.py", line 301, in 
  File "/dwelling/teresa/SiamMask/instruments/train_siammask.py", line 169, in important
    prepare(train_loader, dist_model, optimizer, lr_scheduler, args.start_epoch, cfg)
  File "/dwelling/teresa/SiamMask/instruments/train_siammask.py", line 255, in prepare
  File "/dwelling/teresa/anaconda3/envs/siammask/lib/python3.6/site-packages/torch/tensor.py", line 93, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "/dwelling/teresa/anaconda3/envs/siammask/lib/python3.6/site-packages/torch/autograd/__init__.py", line 90, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: invalid argument 4: anticipating vector of indices at /pytorch/aten/src/THC/generic/THCTensorIndex.cu:16