For someone who meet the same problem, this is a snippet that may help
        a_mean = a_mean/0.1
        a_mean = a_mean.long().view(h*w)
        a_mean = torch.clamp(a_mean, min=0, max=ref_tensor.size(0))
        a_mean = a_mean.view(-1)
        out_tensor = ref_tensor.index_select(dim=0, index=a_mean.data)