SASRec.pytorch icon indicating copy to clipboard operation
SASRec.pytorch copied to clipboard

Predicting only for a single user sequence

Open shanian opened this issue 5 years ago • 7 comments

Is there a way that predict method works in the following way : (This can be used for real time cases)

Given a new user sequence of items that the user interacted with in real time and the model created based on the training data, a set of recommended items with their scores returns.

P.S https://github.com/THUwangcy/ReChorus is also SASRec Pytorch implementation but it is not easy to use it for the single user. Thanks, Sara

shanian avatar Oct 06 '20 14:10 shanian

@shanian Hi Sara, thx for your information, if I understand 'the following way' you presented correctly, are you looking for the stuff inside the 'evaluate' functions? The part is the same as in original tensorflow implementation and in this pytorch version, do inference for users one by one:

https://github.com/pmixer/SASRec.pytorch/blob/dc41b6238c1c4cc90fbd186e387111aab86173f2/main.py#L98

https://github.com/pmixer/SASRec.pytorch/blob/dc41b6238c1c4cc90fbd186e387111aab86173f2/utils.py#L140

pmixer avatar Oct 06 '20 16:10 pmixer

@pmixer Thanks for your quick reply. Yes I like to know how this can be leverage to implement a real life scenario. (When the model is trained and saved.) a new user with some sequences of actions is given and the predict function returns a list of relevant items as the next items for a given user. I will check the part you mentioned.

Thanks again, Sara

shanian avatar Oct 06 '20 17:10 shanian

@shanian thx, I think that's what you need, its a simple trick to make batch_size=1 for online inference(just put what you wan to rank(item ids) to item_idx would help you implement it), I also included a flag to only load the trained model and do inference in the script:

https://github.com/pmixer/SASRec.pytorch/blob/dc41b6238c1c4cc90fbd186e387111aab86173f2/main.py#L62

BTW, this kind of approach is of low performance, pls check serving frameworks if you really hope to do it for online services:

https://github.com/pytorch/serve

https://github.com/triton-inference-server

pmixer avatar Oct 06 '20 17:10 pmixer

@pmixer Thanks for your help :)

shanian avatar Oct 06 '20 17:10 shanian

I also have a question about online inference. I notice that the evaluate() function in the training process randomly selects 100 items and then makes use of "argsort()" to rank the predictions. But how to recommend top10 items to a new user with his historic items sequence as input? For example, if there are 2000 items in total, should I use [1,2,...,2000] as the "item_idx"? (I have encountered difficulties in using the trained model to predict a new sequence.)

Thanks

Jieni05 avatar Aug 05 '21 08:08 Jieni05

input

argsort is just a trick for computing NDCG, pls code your own methods for customized requirements, no need to reuse current evaluate function.

pmixer avatar Aug 06 '21 01:08 pmixer

@laodanhuang I think for your case you need use [1,2,...,2000] as the "item_idx" (all your item in your dataset) and then choose top 10 items.

NicholasLea avatar Jul 05 '22 01:07 NicholasLea