mquad / sars_tutorial

Repository for the tutorial on Sequence-Aware Recommender Systems held at TheWebConf 2019 and ACM RecSys 2018

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

recommend function in GRU4REC RNN - use only the last itme?

eveTu opened this issue · comments

commented

Hi,
Thanks for this wonderful tutorial as a beginner in this field I found it very helpful.

A question about the predict function, I suppose in sequence models like RNN the items and the direction has a meaning.
But I saw in the RNN recommender recommend function that the "pred" variable learn only from one item at a time? and save the prediction of the last one.

    def recommend(self, user_profile, user_id=None):
        if not self.personalized:
            **for item in user_profile:
                pred = self.model.predict_next_batch(np.array([self.pseudo_session_id]),
                                                     np.array([item]),
                                                     batch=1)**
        # sort items by predicted score
        pred.sort_values(0, ascending=False, inplace=True)
        # increase the psuedo-session id so that future call to recommend() won't be connected
        self.pseudo_session_id += 1
        # convert to the required output format
        return [([x.index], x._2) for x in pred.reset_index().itertuples()]

Thanks

Hi @eveTu , happy to hear that you find this tutorial helpful! :-)

Regarding your question, the predict_next_batch in the RNN models that we considered in this codebase (GRU4Rec and HGRU4Rec) update the hidden state of the network every time an item in the sequence is passed to it.
Different sequences of items will lead to different hidden states and hence to different predictions.

The recommend() function in this case is "manually unrolling" the sequence of items in the user profile, passing each of them to predict_next_batch() in their original order to update the inner state of the RNN, and finally return the predictions returned after the last item in the user profile.

The hidden state of the RNN is automatically reset when the session identifier changes, for this reason we resorted on "pseudo session ids" to ensure that different calls to the recommend function are in fact independent.

That said, this way of operating depends heavily on the aforementioned two libraries and their batching mechanism. I suggest to take a look at the corresponding two papers to understand how and why they were built in such way.

* Recurrent Neural Networks with Top-k Gains for Session-based Recommendations, Hidasi and Karatzoglou, CIKM 2018
* Personalizing Session-based Recommendation with Hierarchical Recurrent Neural Networks, Quadrana et al, Recsys 2017

Hope that helps!

commented

Thanks a lot
The answer really helped me