recpack.algorithms.loss_functions.bpr_max_loss

recpack.algorithms.loss_functions.bpr_max_loss(positive_scores: torch.Tensor, negative_scores: torch.Tensor, reg: float = 1.0) torch.Tensor

Bayesian Personalized Ranking Max Loss.

This is a differentiable approximation to the BPR loss between the target item and the negative sample with the highest score, with an added regularization term. It can be defined as:

\[L_{bpr-max} = -\log \sum\limits_{j=1}^{N_S} s_j \sigma(r_i - r_j) + \lambda \sum\limits_{j=1}^{N_S} s_j r_j^2\]

where \(N_S\) is the number of negative samples, \(r_i\) is the target score and \(r_j\) is the score given to the sampled negative. The BPR loss between target score and the maximum sampled score is approximated by computing a softmax distribution over the negative samples and using the softmax values \(s_j\) as weights.

See the 2018 paper “Recurrent Neural Networks with Top-K Gains for Session-based Recommendations” by Hidasi et al. for the motivation behind these changes to the original BPR loss.

Parameters
  • positive_scores (torch.Tensor) – Output values assigned to positive samples

  • negative_scores (torch.Tensor) – Output values assigned to negative samples

  • reg (float, optional) – Regularization weight, defaults to 1.0

Returns

Computed BPR Max Loss

Return type

torch.Tensor