Principle:Recommenders team Recommenders Negative Sampling For Implicit Feedback
| Knowledge Sources | |
|---|---|
| Domains | Recommender Systems, Implicit Feedback, Data Preparation |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Negative sampling for implicit feedback converts observed user-item interactions into positive training pairs and samples unobserved user-item pairs as negative examples, enabling pointwise or pairwise learning for recommendation models.
Description
In explicit feedback settings (e.g., star ratings), the training signal is clear: a user rated an item 4 out of 5. In implicit feedback settings (clicks, views, purchases), only positive signals are observed -- there is no explicit indication that a user dislikes an item. The absence of an interaction could mean the user is unaware of the item, not that they dislike it.
To train a binary classifier (observed vs. unobserved), we need negative examples. Negative sampling constructs these by randomly selecting user-item pairs that were not observed in the training data. These unobserved pairs are assigned a label of 0 (negative), while observed interactions receive a label of 1 (positive).
The negative-to-positive ratio (n_neg) is a critical hyperparameter. For each positive interaction, n_neg negative items are sampled for that user. Higher ratios provide more negative signal but increase training time and can shift the model toward predicting everything as negative. The original NCF paper (He et al., 2017) uses n_neg=4 for training.
For evaluation, a separate and typically larger set of negatives is sampled per test interaction (n_neg_test, often 100). The model ranks the true positive item against these negatives, and metrics like Hit Rate and NDCG measure how well the model surfaces the positive item.
Negative samples can be drawn with or without replacement. Sampling without replacement ensures all negatives for a user are distinct items, but requires that the item catalog is large enough relative to n_neg.
Usage
Use negative sampling whenever training a recommendation model on implicit feedback data. This includes neural collaborative filtering (NCF), Bayesian Personalized Ranking (BPR), and any model that frames recommendation as a classification or ranking problem over implicit signals. The n_neg parameter should be tuned as a hyperparameter; common values range from 1 to 10 for training.
Theoretical Basis
Pointwise Learning with Negative Sampling
Given a set of observed interactions R+ and a sampled set of unobserved interactions R-, the model minimizes binary cross-entropy loss:
L = - sum_{(u,i) in R+} log(y_hat(u,i)) - sum_{(u,j) in R-} log(1 - y_hat(u,j))
where y_hat(u,i) is the model's predicted probability that user u interacts with item i.
Sampling Procedure
For each user u:
positive_items = {i : (u, i) in training_data}
all_items = {1, 2, ..., N}
candidate_negatives = all_items - positive_items
For each positive item i in positive_items:
sample n_neg items from candidate_negatives
yield (u, i, 1) # positive pair
yield (u, neg_1, 0), ..., (u, neg_k, 0) # negative pairs
Evaluation Protocol
For evaluation, each test interaction (u, i) is ranked against n_neg_test randomly sampled negatives:
test_batch(u) = {(u, i_true, 1)} union {(u, j_1, 0), ..., (u, j_100, 0)}
rank = position of i_true when sorted by predicted score descending
HR@K = 1 if rank <= K else 0
NDCG@K = 1 / log2(rank + 1) if rank <= K else 0
This follows the leave-one-out evaluation protocol from the original NCF paper.