Bayesian Personalized Ranking (BPR) System

From GM-RKB
Jump to navigation Jump to search

A Bayesian Personalized Ranking (BPR) System is a personalized ranking system that applies a BPR algorithm to solve a Bayesian Personalized Ranking task).



References

2016b

def optimizeMF(ratings, rank=10, nb_iter=10, nb_partitions=4,
              num_samples=100000, l2_reg=0.001, alpha=0.1,
              negative_samples=30):
   """optimize BPR for Matrix Factorization
   Args:
   -----
       ratings: RDD of (user, item) implicit interactions
       rank: latent factor dimension
       nb_iter: how many iterations of SGD
       nb_partitions: how many user partitions to distribute
       num_samples: |D_s| from the paper
       negative_samples: how many negative samples per positive example
       l2_reg: regularization parameter
       alpha: learning rate
   Returns:
   --------
       (userMat, itemMat)
   """

2016

   import numpy
   import tensorflow as tf
   import os
   import random
   from collections import defaultdict
   
# ...
def bpr_mf(user_count, item_count, hidden_dim): u = tf.placeholder(tf.int32, [None]) i = tf.placeholder(tf.int32, [None]) j = tf.placeholder(tf.int32, [None])
with tf.device(\"/cpu:0\"): user_emb_w = tf.get_variable(\"user_emb_w\", [user_count+1, hidden_dim], initializer=tf.random_normal_initializer(0, 0.1)) item_emb_w = tf.get_variable(\"item_emb_w\", [item_count+1, hidden_dim], initializer=tf.random_normal_initializer(0, 0.1)) item_b = tf.get_variable(\"item_b\", [item_count+1, 1], initializer=tf.constant_initializer(0.0))
u_emb = tf.nn.embedding_lookup(user_emb_w, u) i_emb = tf.nn.embedding_lookup(item_emb_w, i) i_b = tf.nn.embedding_lookup(item_b, i) j_emb = tf.nn.embedding_lookup(item_emb_w, j) j_b = tf.nn.embedding_lookup(item_b, j)
# MF predict: u_i > u_j x = i_b - j_b + tf.reduce_sum(tf.mul(u_emb, (i_emb - j_emb)), 1, keep_dims=True)
# AUC for one user: # reasonable iff all (u,i,j) pairs are from the same user # # average AUC = mean( auc for each user in test set) mf_auc = tf.reduce_mean(tf.to_float(x > 0))
l2_norm = tf.add_n([ tf.reduce_sum(tf.mul(u_emb, u_emb)), tf.reduce_sum(tf.mul(i_emb, i_emb)), tf.reduce_sum(tf.mul(j_emb, j_emb)) ]).
regulation_rate = 0.0001 bprloss = regulation_rate * l2_norm - tf.reduce_mean(tf.log(tf.sigmoid(x)))
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(bprloss) return u, i, j, mf_auc, bprloss, train_op"
# ...
with tf.Graph().as_default(), tf.Session() as session: u, i, j, mf_auc, bprloss, train_op = bpr_mf(user_count, item_count, 20) session.run(tf.initialize_all_variables()) for epoch in range(1, 11): _batch_bprloss = 0 # …