With implicit feedback we only see which items each user interacted with. BPR learns from triples , item selected by and item not, by maximising with .
sample_triples(user_pos, n_items, n, rng): user_pos is a list of lists (the positive items of each user, every list non-empty). For each of n triples: u = rng.integers(len(user_pos)), i = rng.choice(sorted(user_pos[u])), then draw j = rng.integers(n_items) repeatedly until j is not a positive of u. Return a list of (u, i, j) tuples of ints.
bpr_step(U, V, triples, lr, lam): one SGD pass over the triples in order (work on copies). For each, with and , record the loss and update, using the values from before the update, , , . Return (U, V, loss) with loss the mean recorded loss.
hinge_rank_loss(s_pos, s_neg, margin): the mean of .
per_user_auc(U, V, train_pos, test_pos): for each user with at least one test positive, the fraction of pairs (test positive, negative) in which the positive scores higher (ties count 0.5), where negatives are items in neither the user's train nor test positives; return the mean over those users.