Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RankHingeLoss代码逻辑不合理 #159

Open
wenjunyang opened this issue Mar 29, 2021 · 2 comments
Open

RankHingeLoss代码逻辑不合理 #159

wenjunyang opened this issue Mar 29, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@wenjunyang
Copy link

wenjunyang commented Mar 29, 2021

描述

RankHingeLoss的forward方法计算逻辑是:先计算负样本的cosine均值,再调用margin_ranking_loss计算loss。这样是不合理的,因为均值的margin_ranking_loss不等于margin_ranking_loss的均值。我尝试更改后,在模型效果上有很大提升。

fix后的参考代码

def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor):
    """
    Calculate rank hinge loss.

    :param y_pred: Predicted result.
    :param y_true: Label.
    :return: Hinge loss computed by user-defined margin.
    """
    y_pos = y_pred[::(self.num_neg + 1), :]
    y_neg = []
    for neg_idx in range(self.num_neg):
        neg = y_pred[(neg_idx + 1)::(self.num_neg + 1), :]
        y_neg.append(neg)
    r = torch.mean(torch.stack([F.margin_ranking_loss(y_pos, neg, torch.ones_like(y_pos), margin=self.margin) for neg in y_neg]))
    return r
@wenjunyang wenjunyang added the bug Something isn't working label Mar 29, 2021
@Chriskuei
Copy link
Member

@wenjunyang could you open a PR to fix the bug?

@wenjunyang
Copy link
Author

@wenjunyang could you open a PR to fix the bug?

ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants