teddykoker / torchsort

Fast, differentiable sorting and ranking in PyTorch
https://pypi.org/project/torchsort/
Apache License 2.0
765 stars 33 forks source link

Accuracy sensitive to input size and regularization strength #74

Closed Forrest-110 closed 8 months ago

Forrest-110 commented 1 year ago

Dear authors, you have done a great job! But I met some difficulties when dealing with large size of input (1k-5k).

I have a list of numbers ranging in [0,1], and the list size is about 1k to 5k. And I'm trying to find a suitable regularization strength to get accurate ranking results.

My experiment codes are listed below

import torchsort
import torch
from matplotlib import pyplot as plt
def test_torchsort():
    x = torch.rand(1, 1000).cuda()
    order=torchsort.soft_rank(x,regularization_strength=1e-7).type(torch.int32)
    gt_order=torch.argsort(torch.argsort(x,dim=1),dim=1)+1
    error=torch.abs(order-gt_order).sum().item()
    return error

def test_stablity():
    errors=[]
    error_cnt=0
    for i in range(100):
        error=test_torchsort()
        errors.append(error)
        if error>0:
            error_cnt+=1
    accuracy=1-error_cnt/len(errors)
    print(accuracy)
    plt.hist(errors)
    plt.show()
if __name__ == '__main__':
    test_stablity()

The things is:

That's exactly the issue. If I have to find a regularization strength for every input size, it could be quite impracticable. Could you help me?

teddykoker commented 8 months ago

Hey @Forrest-110, apologies for the very late reply I must have missed this. If you are still working on this, do you notice a similar trend with your downstream performance when doing inference? Typically you would only want to use soft rank/sort while training and the gradients are needed. Once trained, you can just use torch.argsort as you do above. I would recommend tuning the regularization using a cross validation set.

Forrest-110 commented 8 months ago

这是来自QQ邮箱的自动回复邮件。   您好,邮件已收到,但我无法亲自回复您的邮件。不久即速回。

Forrest-110 commented 8 months ago

这是来自QQ邮箱的自动回复邮件。   您好,邮件已收到,但我无法亲自回复您的邮件。不久即速回。