LibRecommender icon indicating copy to clipboard operation
LibRecommender copied to clipboard

Advice for the Implementation of BPR for Importance Sampling

Open adebiasio-estilos opened this issue 3 years ago • 7 comments

Hi All, I would like to implement in LibRecommender BPR with Importance Sampling.

  • http://staff.ustc.edu.cn/~liandefu/paper/pris.pdf I have already modified the Sampling function. What should i do to modify the loss function of BPR?

Thanks in advance Alvise

adebiasio-estilos avatar Feb 08 '23 17:02 adebiasio-estilos

Moreover, taking for example in consideration BPR's current implementation:

  • https://github.com/massquantity/LibRecommender/blob/master/libreco/algorithms/bpr.py

how can I print at each forward pass the various loss variables?

adebiasio-estilos avatar Feb 08 '23 21:02 adebiasio-estilos

Thanks for implementing this! I've seen the paper you mentioned roughly, and I'll assume you're using TensorFlow1. It seems that the main difference is they add a weight(w_l) parameter in the original BPR loss. So you can add a hyperparameter such as importance_sampling to let users choose whether to use it. Then you will implement the weight(w_l).

At last, in bpr.py, the code may look like this:

if self.importance_sampling:
    self.bpr_loss = w * tf.log_sigmoid(item_diff)
else:
    self.bpr_loss = tf.log_sigmoid(item_diff)

massquantity avatar Feb 09 '23 05:02 massquantity

What do you mean by various loss variables? Training loss or model's variables?

massquantity avatar Feb 09 '23 05:02 massquantity

Thank you for your help @massquantity ! The methodology you propose looks good!

adebiasio-estilos avatar Feb 09 '23 07:02 adebiasio-estilos

What do you mean by various loss variables? Training loss or model's variables?

Yes, I mean the TF model's variables during the graph execution. How are you used to debug the TF1 code?

adebiasio-estilos avatar Feb 09 '23 07:02 adebiasio-estilos

For example, If I want to get the self.bpr_loss, run sess.run in the training function, which can get the variable and its shape.

for data in data_generator(shuffle, self.batch_size):
    self.sess.run(
        self.training_op,
        feed_dict={
            self.model.user_indices: data[0],
            self.model.item_indices_pos: data[1],
            self.model.item_indices_neg: data[2],
        },
    )

    feed_dict = {
        self.model.user_indices: data[0],
        self.model.item_indices_pos: data[1],
        self.model.item_indices_neg: data[2],
    }
    print(self.sess.run(self.model.bpr_loss, feed_dict=feed_dict).shape)
    print(self.sess.run(self.model.bpr_loss, feed_dict=feed_dict))

If you can't get a variable from self.model, just add a self. before the variable you want to get in your implementation.

massquantity avatar Feb 09 '23 16:02 massquantity

Thank you very much, it works!!!

adebiasio-estilos avatar Feb 09 '23 19:02 adebiasio-estilos