massquantity / LibRecommender

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

save and load torch model in lightgcn #455

Open sorushsaghari opened 4 months ago

sorushsaghari commented 4 months ago

i have a scenario wich i need to use lightgcn model in a GAN later, so after training and saving i want to load lightgcn and use model.torchmodel.parameters() to tune this model in a GAN and i dont want to use the fit interface i the model. while loading model in inference_only mode there is no torch model to train, and if i try to use rebuild_model functionality i will get :

[~/.local/lib/python3.10/site-packages/libreco/bases/embed_base.py:188](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/bases/embed_base.py:188), in EmbedBase.predict(self, user, item, cold_start, inner_id)
    [163](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/bases/embed_base.py:163) def predict(self, user, item, cold_start="average", inner_id=False):
    [164](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/bases/embed_base.py:164)     """Make prediction(s) on given user(s) and item(s).
    [165](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/bases/embed_base.py:165) 
    [166](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/bases/embed_base.py:166)     Parameters
   (...)
    [186](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/bases/embed_base.py:186)         Predicted scores for each user-item pair.
    [187](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/bases/embed_base.py:187)     """
--> [188](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/bases/embed_base.py:188)     return predict_from_embedding(self, user, item, cold_start, inner_id)

File [~/.local/lib/python3.10/site-packages/libreco/prediction/predict.py:39](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/prediction/predict.py:39), in predict_from_embedding(model, user, item, cold_start, inner_id)
     [37](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/prediction/predict.py:37) user, item = convert_id(model, user, item, inner_id)
     [38](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/prediction/predict.py:38) unknown_num, unknown_index, user, item = check_unknown(model, user, item)
---> [39](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/prediction/predict.py:39) preds = np.sum(model.user_embeds_np[user] * model.item_embeds_np[item], axis=1)
     [40](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/prediction/predict.py:40) return normalize_prediction(preds, model, cold_start, unknown_num, unknown_index)

TypeError: 'NoneType' object is not subscriptable

error beacuse the embedings are none and i have to call fit function. but i want the pretrained embeddings. what can i do for that ?

massquantity commented 4 months ago

You can set inference_only to False, then rebuild_model.

Setting inference_only to True will only save the embeddings and drop the entire torch model.

sorushsaghari commented 4 months ago

You can set inference_only to False, then rebuild_model.

Setting inference_only to True will only save the embeddings and drop the entire torch model.

as i mentioned when is set inference_only and rebuilt model i get the error i mentioned while calling predict, until i have not called fit function. this is the code:

lightgcn.save(
    path="lgc_model", model_name="lightgcn_model", manual=True, inference_only=False
)
# %%
data_info.save(
    path="lgc_model",
    model_name="lightgcn_model",
)
# %%
import tensorflow as tf

def reset_state(name):
    tf.compat.v1.reset_default_graph()
    print("\n", "=" * 30, name, "=" * 30)
#%%
reset_state("retrain")
#%%
_, n_data_info = DatasetPure.merge_trainset(
        train_dat, data_info
)

model = LightGCN(
    task="ranking",
    data_info=n_data_info,
    loss_type="bpr",
    embed_size=16,
    n_epochs=2,
    lr=1e-3,
    batch_size=2048,
    num_neg=10,
    device="cuda",

)
# %%
model.rebuild_model( path="lgc_model", model_name="lightgcn_model",)

# %%
##
model.predict(user=[0,10], item=[12])

and again the error:

def predict(self, user, item, cold_start="average", inner_id=False):
    [164](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/bases/embed_base.py:164)     """Make prediction(s) on given user(s) and item(s).
    [165](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/bases/embed_base.py:165) 
    [166](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/bases/embed_base.py:166)     Parameters
   (...)
    [186](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/bases/embed_base.py:186)         Predicted scores for each user-item pair.
    [187](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/bases/embed_base.py:187)     """
--> [188](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/bases/embed_base.py:188)     return predict_from_embedding(self, user, item, cold_start, inner_id)

File [~/.local/lib/python3.10/site-packages/libreco/prediction/predict.py:39](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/prediction/predict.py:39), in predict_from_embedding(model, user, item, cold_start, inner_id)
     [37](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/prediction/predict.py:37) user, item = convert_id(model, user, item, inner_id)
     [38](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/prediction/predict.py:38) unknown_num, unknown_index, user, item = check_unknown(model, user, item)
---> [39](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/prediction/predict.py:39) preds = np.sum(model.user_embeds_np[user] * model.item_embeds_np[item], axis=1)
     [40](https://vscode-remote+wsl-002bubuntu-002d22-002e04.vscode-resource.vscode-cdn.net/home/sorush/workspace/uni/thesis/~/.local/lib/python3.10/site-packages/libreco/prediction/predict.py:40) return normalize_prediction(preds, model, cold_start, unknown_num, unknown_index)

TypeError: 'NoneType' object is not subscriptable
massquantity commented 4 months ago

Try calling model.set_embeddings() after rebuild_model and before predict.