massquantity / LibRecommender

Versatile End-to-End Recommender System
https://librecommender.readthedocs.io/
MIT License
352 stars 62 forks source link

Logging DeepFM Model #442

Closed ManishaNumtra closed 6 months ago

ManishaNumtra commented 7 months ago

Hi While logging models of deepFM. We are unable to save a pb file. Due to which we are unable to further load the model for predictions. Could you please help us on how to log the model using tensorflow.

massquantity commented 7 months ago

Please provide all your code and error messages here.

ManishaNumtra commented 7 months ago

Hi,

Thank you for reaching back. Please find below code used for training and saving the model:

import pandas as pd from libreco.algorithms import WideDeep,DeepFM from libreco.data import random_split, DatasetFeat,DatasetPure,TransformedSet from libreco.evaluation import evaluate

-------------------------------Data Preprocessing-------------------------------------

df1 = pd.read_csv('MovieData.csv', sep=',', nrows=10000) df = df1.copy() df = df.rename(columns={'UserId':'user','MovieId':'item'}) df.insert(2,'label',1) df['label'] = df['Rating'] train_data, data_info = DatasetFeat.build_trainset(df,user_col=['Age'], item_col=['Popularity', 'Runtime', 'vote_average','Genres'], sparse_col=['Genres'], dense_col=['Popularity', 'Runtime', 'vote_average','Age'])

--------------------Model training--------------------------------------

model = DeepFM(data_info =data_info, task='ranking', loss_type='cross_entropy', embed_size=16, n_epochs=20, lr=0.001, lr_decay=False, epsilon=1e-05, batch_size=256, sampler='random', num_neg=1, use_bn=True, hidden_units=(128, 64, 32), multi_sparse_combiner='sqrtn') history = model.fit(train_data,neg_sampling=True, verbose=1, shuffle=True, eval_data=None, metrics=None, k=10, eval_batch_size=8192, eval_user_num=None, num_workers=0)

--------------------------Model Logging (Type1)--------------------------------

model.save(path="model_path", model_name="deepfm_model", manual=False, inference_only=False) The following files have been saved using above logging step:

  1. deepfm_model_default_recs.npz
  2. deepfm_model_hyper_parameters
  3. checkpoint
  4. deepfm_model_tf.data-00000-of-00001
  5. deepfm_model_tf.index
  6. deepfm_model_tf.meta

---------------------Model Logging (Type2)--------------------------------

I have also used below method to log the model. However, this method did not save npz or hyper_parameters files.:

from libreco.utils.save_load import save_tf_model. session=model.sess path = "DeepFmtest" model_name = "DeepFm_model" save_tf_model(session,path, model_name)

--------------------Loading Model---------------------------------------

np.load('/xxxxxxxxxxxxxxxxx/model_path/deepfm_model_default_recs.npz') I used above code to load the model. However, I would like to know if above line is the correct method to load the model. Also, is there a possibility to save the model directly using tensorflow or mlflow.

Thank you once again for helping out.

Manisha

massquantity commented 7 months ago

Why not use the official API to load the model?

The source code uses tf.train.Saver to save the model, so I assume this is the direct tensorflow way?

I've seen the source code in mlflow on logging model, and I don't think it is compatible with models in this library.

ManishaNumtra commented 6 months ago

Hi Thank you for the support. I found a solution using mlflow.pyfunc to log as a customized model and could log and load the model for predictions.