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

update map at k loss #61

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion libauc/losses/auc.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,13 +787,14 @@ def forward(self, y_pred, y_true, task_id=[], auto=True, **kwargs):

class meanAveragePrecisionLoss(torch.nn.Module):
r"""
Mean Average Precision loss based on squared-hinge surrogate loss to optimize mAP. This is an extension of :obj:`~libauc.losses.APLoss`.
Mean Average Precision loss based on squared-hinge surrogate loss to optimize mAP and mAP@k. This is an extension of :obj:`~libauc.losses.APLoss`.

Args:
data_len (int): total number of samples in the training dataset.
num_labels (int): number of unique labels(tasks) in the dataset.
margin (float, optional): margin for the squared-hinge surrogate loss (default: ``1.0``).
gamma (float, optional): parameter for the moving average estimator (default: ``0.9``).
top_k (int, optional): If given, only top k items will be considered for optimizing mAP@k.
surr_loss (str, optional): type of surrogate loss to use. Choices are 'squared_hinge', 'squared',
'logistic', 'barrier_hinge' (default: ``'squared_hinge'``).

Expand All @@ -819,6 +820,7 @@ def __init__(self,
num_labels,
margin=1.0,
gamma=0.9,
top_k=-1,
surr_loss='squared_hinge',
device=None):
super(meanAveragePrecisionLoss, self).__init__()
Expand All @@ -833,6 +835,7 @@ def __init__(self,
self.margin = margin
self.gamma = gamma
self.surrogate_loss = get_surrogate_loss(surr_loss)
self.top_k = top_k

def forward(self, y_pred, y_true, index, task_id=[], **kwargs):
y_pred = check_tensor_shape(y_pred, (-1, self.num_labels))
Expand All @@ -856,6 +859,9 @@ def forward(self, y_pred, y_true, index, task_id=[], **kwargs):
self.u_all[idx][index_i] = (1 - self.gamma) * self.u_all[idx][index_i] + self.gamma * (sur_loss.mean(1, keepdim=True)).detach()
self.u_pos[idx][index_i] = (1 - self.gamma) * self.u_pos[idx][index_i] + self.gamma * (pos_sur_loss.mean(1, keepdim=True)).detach()
p_i = (self.u_pos[idx][index_i] - (self.u_all[idx][index_i]) * pos_mask) / (self.u_all[idx][index_i] ** 2) # size of p_i: len(f_ps)* len(y_pred)
if self.top_k > -1:
selector = torch.sigmoid(self.top_k - sur_loss.sum(dim=0, keepdim=True).clone())
p_i *= selector
p_i.detach_()
loss = torch.mean(p_i * sur_loss)
total_loss += loss
Expand Down