Loss functions¶
Loss functions for recommender models.
The pointwise, BPR, and hinge losses are a good fit for implicit feedback models trained through negative sampling.
The regression and Poisson losses are used for explicit feedback models.

spotlight.losses.
adaptive_hinge_loss
(positive_predictions, negative_predictions, mask=None)[source]¶ Adaptive hinge pairwise loss function. Takes a set of predictions for implicitly negative items, and selects those that are highest, thus sampling those negatives that are closes to violating the ranking implicit in the pattern of user interactions.
Approximates the idea of weighted approximaterank pairwise loss introduced in [2]
Parameters:  positive_predictions (tensor) – Tensor containing predictions for known positive items.
 negative_predictions (tensor) – Iterable of tensors containing predictions for sampled negative items. More tensors increase the likelihood of finding rankingviolating pairs, but risk overfitting.
 mask (tensor, optional) – A binary tensor used to zero the loss from some entries of the loss tensor.
Returns: The mean value of the loss function.
Return type: loss, float
References
[2] Weston, Jason, Samy Bengio, and Nicolas Usunier. “Wsabie: Scaling up to large vocabulary image annotation.” IJCAI. Vol. 11. 2011.

spotlight.losses.
bpr_loss
(positive_predictions, negative_predictions, mask=None)[source]¶ Bayesian Personalised Ranking [1] pairwise loss function.
Parameters:  positive_predictions (tensor) – Tensor containing predictions for known positive items.
 negative_predictions (tensor) – Tensor containing predictions for sampled negative items.
 mask (tensor, optional) – A binary tensor used to zero the loss from some entries of the loss tensor.
Returns: The mean value of the loss function.
Return type: loss, float
References
[1] Rendle, Steffen, et al. “BPR: Bayesian personalized ranking from implicit feedback.” Proceedings of the twentyfifth conference on uncertainty in artificial intelligence. AUAI Press, 2009.

spotlight.losses.
hinge_loss
(positive_predictions, negative_predictions, mask=None)[source]¶ Hinge pairwise loss function.
Parameters:  positive_predictions (tensor) – Tensor containing predictions for known positive items.
 negative_predictions (tensor) – Tensor containing predictions for sampled negative items.
 mask (tensor, optional) – A binary tensor used to zero the loss from some entries of the loss tensor.
Returns: The mean value of the loss function.
Return type: loss, float

spotlight.losses.
logistic_loss
(observed_ratings, predicted_ratings)[source]¶ Logistic loss for explicit data.
Parameters:  observed_ratings (tensor) – Tensor containing observed ratings which should be +1 or 1 for this loss function.
 predicted_ratings (tensor) – Tensor containing rating predictions.
Returns: The mean value of the loss function.
Return type: loss, float

spotlight.losses.
pointwise_loss
(positive_predictions, negative_predictions, mask=None)[source]¶ Logistic loss function.
Parameters:  positive_predictions (tensor) – Tensor containing predictions for known positive items.
 negative_predictions (tensor) – Tensor containing predictions for sampled negative items.
 mask (tensor, optional) – A binary tensor used to zero the loss from some entries of the loss tensor.
Returns: The mean value of the loss function.
Return type: loss, float

spotlight.losses.
poisson_loss
(observed_ratings, predicted_ratings)[source]¶ Poisson loss.
Parameters:  observed_ratings (tensor) – Tensor containing observed ratings.
 predicted_ratings (tensor) – Tensor containing rating predictions.
Returns: The mean value of the loss function.
Return type: loss, float

spotlight.losses.
regression_loss
(observed_ratings, predicted_ratings)[source]¶ Regression loss.
Parameters:  observed_ratings (tensor) – Tensor containing observed ratings.
 predicted_ratings (tensor) – Tensor containing rating predictions.
Returns: The mean value of the loss function.
Return type: loss, float