forked from js-ish/DOoC
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request js-ish#14 from js-ish/feat-listrank
improve learn to rank
- Loading branch information
Showing
7 changed files
with
64 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import torch | ||
import torch.nn as nn | ||
|
||
|
||
class ListNetLoss(nn.Module): | ||
def __init__(self, reduction: str = 'mean') -> None: | ||
super().__init__() | ||
assert reduction in ['mean', 'sum'] | ||
self.reduction = reduction | ||
|
||
def forward(self, predict: torch.Tensor, target: torch.Tensor) -> torch.Tensor: | ||
out = - (target.softmax(dim=-1) * predict.log_softmax(dim=-1)) | ||
return getattr(out, self.reduction)() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import torch | ||
from dooc import loss | ||
|
||
def test_ListNetLoss(): | ||
predict = torch.randn(5, 3) | ||
target = torch.randn(5, 3) | ||
loss_mean = loss.ListNetLoss(reduction='mean') | ||
mean = loss_mean(predict, target) | ||
loss_sum = loss.ListNetLoss(reduction='sum') | ||
sum = loss_sum(predict, target) | ||
assert sum / 15 == mean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters