pmixer / SASRec.pytorch

PyTorch(1.6+) implementation of https://github.com/kang205/SASRec
Apache License 2.0
349 stars 93 forks source link

Predicting only for a single user sequence #4

Open shanian opened 4 years ago

shanian commented 4 years ago

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

pmixer commented 4 years ago

@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

shanian commented 4 years ago

@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

pmixer commented 4 years ago

@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

shanian commented 4 years ago

@pmixer Thanks for your help :)

Jieni05 commented 3 years ago

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

pmixer commented 3 years ago

input

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

NicholasLea commented 2 years ago

@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.