hidasib / GRU4Rec

GRU4Rec is the original Theano implementation of the algorithm in "Session-based Recommendations with Recurrent Neural Networks" paper, published at ICLR 2016 and its follow-up "Recurrent Neural Networks with Top-k Gains for Session-based Recommendations". The code is optimized for execution on the GPU.
Other
754 stars 223 forks source link

(Question) - How to use all items in a session for prediction? #37

Closed mokarakaya closed 4 years ago

mokarakaya commented 5 years ago

I have a session (sequence) of a user (user_id) sequence is a list of items( [1, 2, 3]) and user_id is an integer

I'd like to predict the next item of the session by using the all items in the session so far. I don't want to use only the last item in the session.

If I understand correctly from the documentation, in order to predict the next item of the session so far, I need to call predict_next_batch subsequently with the items in the session and return the last prediction as below;

    def _predict(self, sequence, user_id, item_ids=None):
        predictions = None
        for item in sequence:
            predictions = self.gru4rec.predict_next_batch([user_id], [item], item_ids, batch=1)
        return predictions.values[:, 0]

Is my understanding correct that the code below uses all the items in the sequence to produce final predictions?

Finally, which one do you think is better? Using all items in the session or only the last item of the session?

Thanks for sharing the code.

hidasib commented 4 years ago

Correct, you can predict the next item to a session by calling predict_next_batch multiple times feeding it the items in your session and the same user (or session) id. Keep in mind that this will be slow, because predictions for every item are computed in every step. predict_next_batch was designed for evaluating the model (by using evaluate_sessions_batch) and not for providing actual predictions (later it was superseded by symbolic_predict (that is used by evaluate_gpu) which is even harder to use for getting the actual predictions). The public version of this algorithm doesn't directly support inferring recommendations to individual sessions (because it is intended to be used for reproducing earlier results, or as a baseline for other session-based algorithms).

In the above code, the first and second parameters of predict_next_batch should be numpy arrays and not lists. It might work with lists of only one ID, but its best to stick with numpy arrays here.

As for which one is better item-to-item or item-to-session recommendations: item-to-item recommendations are not personalized at all and can't differentiate between different intents. Session-based recommenders have some capacity to do and also have some notable practical advantages over fully personalized recommenders as well. So, I'd definitely go with predicting items to the session (in practical scenarios, you might want to limit the maximal length of your session to the last X items of the user).

mokarakaya commented 4 years ago

thanks for the reply.