recommenders-team / recommenders

Best Practices on Recommendation Systems
https://recommenders-team.github.io/recommenders/intro.html
MIT License
18.83k stars 3.07k forks source link

[ASK] Save NCF Model #1735

Open schua16 opened 2 years ago

schua16 commented 2 years ago

Description

I want to save the entire NCF model and use it without requiring the original model building code to run. How can I do that? The example in the deep dive notebook only shows how to save the model for further training which is not what I am looking for. Appreciate any reponses.

Other Comments

Squderini commented 1 year ago

I have the same problem. I want to save model and then want to load it without retraining for prediction

jamie613 commented 1 year ago

I used model.save(path) to save the model, and got 4 files:

checkpoint model.ckpt.data-00000-of-00001 model.ckpt.index model.ckpt.meta

To load the model, I tried model = NCF(parameters) model.load(neumf_dir = model_path)

it worked, but when I run model.predict()

There's error: AttributeError: 'NCF' object has no attribute 'user2id'

Have no idea how to fix this.

MHumza3656 commented 1 year ago

It would help if you fed it data parameters as shown in this notebook example https://github.com/microsoft/recommenders/blob/main/examples/02_model_collaborative_filtering/ncf_deep_dive.ipynb 3.5.2 Load pre-trained GMF and MLP model for NeuMF

jamie613 commented 1 year ago

Does this mean that one cannot save NeuMF model, but have to train and save the GMF and MLP separately?

MHumza3656 commented 1 year ago

No, you can save the NeuMF model by providing the path model.save('dir_path') and then while loading depending on the type of your model in this case neumf pass it that dir parameter model.load(neumf_dir='dir_path')

Note: you have to create your model before loading with the same parameters, as shown in the example.

jamie613 commented 1 year ago

I still get 'NCF' object has no attribute 'user2id'

What I did:

model.fit(data)
modl.save('dir_path')

then opened a new console

data = NCFDataset(train_file = train_file, test_file = test_file, seed = SEED) #as the saved model
model = NCF ( the same parameters as the saved model)
model.load(neumf_dir='dir_path')
model.predict(users, items, is_list = True)
MHumza3656 commented 1 year ago

After loading you will have to specifically assign these attributes to NCF class

model.user2id = data.user2id model.item2id = data.item2id model.id2user = data.id2user model.id2item = data.id2item

BigOrange3 commented 1 year ago

Thank you!Finally succeeded!