EasyRec icon indicating copy to clipboard operation
EasyRec copied to clipboard

How to output topk items to recommend from DSSM model

Open ss-github-code opened this issue 3 years ago • 10 comments

Can you please provide an example config on how to output topk (say = 5) items after training the DSSM model? I am unable to find this information in the documentation.

ss-github-code avatar Apr 27 '22 06:04 ss-github-code

you need use a vector recall engine, such as faiss。 use user embedding to query item embedding, then get topk items。

poson avatar Apr 27 '22 09:04 poson

Thanks for your quick answer, much appreciated @poson! Faiss requires the full set of vectors; can you please let me know how to get this from EasyRec.

ss-github-code avatar Apr 27 '22 15:04 ss-github-code

You can get all the vectors by predict (https://easyrec.readthedocs.io/en/latest/predict/MaxCompute%20%E7%A6%BB%E7%BA%BF%E9%A2%84%E6%B5%8B.html).

dawn310826 avatar May 05 '22 02:05 dawn310826

first, you should use this script(https://github.com/alibaba/EasyRec/blob/master/easy_rec/python/tools/split_model_pai.py) to split the model into two parts: user part and item part.

chengmengli06 avatar May 05 '22 02:05 chengmengli06

Thanks for the help. But I get an error: File "/home/shiv/Documents/DataScience/EasyRec/easy_rec/python/tools/split_model_pai.py", line 16, in FLAGS = tf.app.flags.FLAGS AttributeError: module 'tensorflow' has no attribute 'app'

And when I add to force it to use Tensorflow v1 by adding the following two lines:

if tf.version >= '2.0': tf = tf.compat.v1

I still don't go too far, please note I am using dssm_on_taobao.cfg.

Traceback (most recent call last): File "/home/shiv/Documents/DataScience/EasyRec/easy_rec/python/tools/split_model_pai.py", line 271, in tf.app.run() File "/home/shiv/anaconda3/envs/RecSys_39/lib/python3.9/site-packages/tensorflow/python/platform/app.py", line 36, in run _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef) File "/home/shiv/anaconda3/envs/RecSys_39/lib/python3.9/site-packages/absl/app.py", line 312, in run _run_main(main, args) File "/home/shiv/anaconda3/envs/RecSys_39/lib/python3.9/site-packages/absl/app.py", line 258, in _run_main sys.exit(main(argv)) File "/home/shiv/Documents/DataScience/EasyRec/easy_rec/python/tools/split_model_pai.py", line 251, in main export( File "/home/shiv/Documents/DataScience/EasyRec/easy_rec/python/tools/split_model_pai.py", line 204, in export saver = tf_saver.Saver() File "/home/shiv/anaconda3/envs/RecSys_39/lib/python3.9/site-packages/tensorflow/python/training/saver.py", line 919, in init self.build() File "/home/shiv/anaconda3/envs/RecSys_39/lib/python3.9/site-packages/tensorflow/python/training/saver.py", line 931, in build self._build(self._filename, build_save=True, build_restore=True) File "/home/shiv/anaconda3/envs/RecSys_39/lib/python3.9/site-packages/tensorflow/python/training/saver.py", line 959, in _build self.saver_def = self._builder._build_internal( # pylint: disable=protected-access File "/home/shiv/anaconda3/envs/RecSys_39/lib/python3.9/site-packages/tensorflow/python/training/saver.py", line 503, in _build_internal saveables = saveable_object_util.validate_and_slice_inputs( File "/home/shiv/anaconda3/envs/RecSys_39/lib/python3.9/site-packages/tensorflow/python/training/saving/saveable_object_util.py", line 356, in validate_and_slice_inputs for converted_saveable_object in saveable_objects_for_op(op, name): File "/home/shiv/anaconda3/envs/RecSys_39/lib/python3.9/site-packages/tensorflow/python/training/saving/saveable_object_util.py", line 219, in saveable_objects_for_op yield ResourceVariableSaveable(variable, "", name) File "/home/shiv/anaconda3/envs/RecSys_39/lib/python3.9/site-packages/tensorflow/python/training/saving/saveable_object_util.py", line 91, in init self.handle_op = var.op.inputs[0] IndexError: tuple index out of range

ss-github-code avatar May 06 '22 01:05 ss-github-code

Hi, I am also confused by this question. Could you please provide documents about split_model_pai.py and how to deploy retrieval model with only user tower and faiss?

kinghuin avatar May 06 '22 09:05 kinghuin

what is your tf version @ss-github-code

chengmengli06 avatar May 09 '22 02:05 chengmengli06

@kinghuin documentation is in preparation, here are the steps:

  1. split_model_pai.py generates two saved_models, one for generating user embedding, and one for generating item embedding. The generated saved_model could be used for serving(using tensor-rt or PAI-EAS).
  2. after getting the saved_models, you can do offline prediction to get the user embeddings ( https://easyrec.readthedocs.io/en/latest/predict/MaxCompute%20%E7%A6%BB%E7%BA%BF%E9%A2%84%E6%B5%8B.html).
  3. for online prediction by deploying user model to PAI-EAS service(https://easyrec.readthedocs.io/en/latest/predict/%E5%9C%A8%E7%BA%BF%E9%A2%84%E6%B5%8B.html).
  4. Item embeddings are usually predicted offline similar to step 2 and are saved in MaxCompute tables, which could then be imported to Hologres(Proxima) for online KNN retrieval. You can also import the embedding vectors into other KNN engines(such as Faiss) for online retrieval.

chengmengli06 avatar May 09 '22 04:05 chengmengli06

Sorry for the delay. My tensorflow version is 2.8

ss-github-code avatar May 17 '22 04:05 ss-github-code

the script could not work under tf2.x, we are fixing it, the reason is that the variable created in tf2.x are different from tf1.x

chengmengli06 avatar Jun 08 '22 06:06 chengmengli06

Could you provide a tf2 version script of this split function?

treper avatar Jul 28 '23 08:07 treper