tensorflow / recommenders

TensorFlow Recommenders is a library for building recommender system models using TensorFlow.
Apache License 2.0
1.84k stars 275 forks source link

How to extract predictions from Multi Task Model #215

Open bellicapelli0 opened 3 years ago

bellicapelli0 commented 3 years ago

I'm following the tutorial here and I am wondering how to get a prediction for a given user. Once training is done, how can I pass a user_id string and receive a list of ranked recommendations? Thanks in advance for the help.

Flipper-afk commented 3 years ago

You can find it in the prediction section of the basic retieval tutorial.

bellicapelli0 commented 3 years ago

Correct me if I'm wrong, but doesn't that only apply to the retrieval part of the multi-task model? How can this be extended to include the ranking part?

maciejkula commented 3 years ago

To get just the ranking predictions you could add the following method to the model:

def ranking_predictions(self, features: Dict[Text, tf.Tensor]) -> tf.Tensor:
    # We pick out the user features and pass them into the user model.
    user_embeddings = self.user_model(features["user_id"])
    # And pick out the movie features and pass them into the movie model.
    movie_embeddings = self.movie_model(features["movie_title"])

    return self.rating_model(
            tf.concat([user_embeddings, movie_embeddings], axis=1)
        )

You could wrap this in @tf.function for efficiency and SavedModel exportability.

Note that to score multiple candidates for a given user you'll need to pass multiple (user, candidate rows here) or else modify the ranking_predictions function to do user representation broadcasting.

Niouscha91 commented 3 years ago

@bellicapelli0 I have the same issue as yours. I want to predict the ranking in the ranking part. Have you found any solution for that?