Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we serialize/store/save the LightFM model? #7

Open
audiodude opened this issue Aug 16, 2024 · 2 comments
Open

Can we serialize/store/save the LightFM model? #7

audiodude opened this issue Aug 16, 2024 · 2 comments

Comments

@audiodude
Copy link
Collaborator

Maybe we can just pickle the model itself after it's been trained?

@siddz415
Copy link
Collaborator

siddz415 commented Oct 4, 2024

user_ids = [0, 0, 1, 1, 2]
movie_ids = [0, 1, 1, 2, 2]
ratings = [5, 4, 5, 4, 5] # Only 4s and 5s for this example

Create sparse interaction matrix

interaction_matrix = coo_matrix((ratings, (user_ids, movie_ids)), shape=(3, 3))

Initialize LightFM model with WARP loss

model = LightFM(loss='warp')

Train the model

model.fit(interaction_matrix, epochs=30, num_threads=2)

Predict scores for User 0

scores = model.predict(0, np.arange(3))
top_items = np.argsort(-scores)
print("Top recommended items for User 0:", top_items)

Evaluate model

from lightfm.evaluation import precision_at_k, auc_score
precision = precision_at_k(model, interaction_matrix, k=5).mean()
auc = auc_score(model, interaction_matrix).mean()

print(f"Precision at k=5: {precision}, AUC Score: {auc}")
something like this?

@siddz415 siddz415 closed this as completed Oct 4, 2024
@audiodude
Copy link
Collaborator Author

I think the pickleing would happen after your model.fit code.

There's also a question of how long it takes to fit the model. If it's under 10 seconds, it might not be worth it to deal with pickle.

@audiodude audiodude reopened this Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants