pythontensorflowrecommendation-enginecollaborative-filtering

Recommender System (SVD) with TensorFlow


I'm trying to create a collaborative filtering algorithm to suggest products to certain users.

I started shortly and started working with TensorFlow (I thought it was sufficiently effective and flexible). I found this code that does what I'm interested in, creates the model and train the user IDs, products, and ratings: https://github.com/songgc/TF-recomm

I launched the code and trained the model.

After training the model I would need to make the predictions, that is, get suggestions for each user so that they can be saved in a DB from which I access with a NODE.js application.

How do I retrieve this list of suggestions for each user when the training is done?

if __name__ == '__main__':
    df_train, df_test=get_data()
    svd(df_train, df_test)
    print("Done!")

Solution

  • You need to modify the prediction part of your code to output top K recommended products. The current code where the prediction is made is :

     embd_user = tf.nn.embedding_lookup(w_user, user_batch, name="embedding_user")
     embd_item = tf.nn.embedding_lookup(w_item, item_batch, name="embedding_item")
     infer = tf.reduce_sum(tf.multiply(embd_user, embd_item), 1)
    

    Here the embed_user is the user embeddings of a particular user and embd_item is for the particular item. So instead of comparing a particular user with a particular item, you need to change it to compare it to all items. The matrix w_item is the embeddings of all items. This can be done by:

     embd_user = tf.nn.embedding_lookup(w_user, user_batch, name="embedding_user")
     # Multiply user embedding of shape: [1 x dim] 
     # with every item embeddings of shape: [item_num, dim], 
     # to produce rank of all items of shape: [item_num]
     predict = tf.matmul(embd_user, w_item, transpose_b=True)
    

    Then you can select the top k index of the maximum in the predicted output.