hand10ryo / PyTorchCML

PyTorchCML is a library of PyTorch implementations of matrix factorization (MF) and collaborative metric learning (CML), algorithms used in recommendation systems and data mining.
MIT License
20 stars 2 forks source link

Way to get scored recommendations? #36

Closed shivamtundele closed 2 years ago

shivamtundele commented 2 years ago

Is there a way to get back predicted top k recommendations from the Evaluator or any other way? For example, user A was recommended movies 1,.....,n=5 (k=5).

shivamtundele commented 2 years ago

Built it real quick as below,

from PyTorchCML import losses, models, samplers, regularizers, evaluators, trainers
import torch, multiprocessing
from itertools import product
import numpy as np
from joblib import Parallel, delayed
from tqdm import tqdm

def cml_get_recommendations(model, test_users, items, k, num_batch, num_threads, return_scores=False):
    batches = np.array_split(test_users, num_batch)
    inputs = tqdm(batches)
    def predict_user(model, test_users, items, k):
        pairs = pd.DataFrame(list(product(test_users, items)), columns = ['user', 'item'])
        pairs['score'] = model.predict(torch.LongTensor(pairs.values)).tolist()
        pairs = pairs.sort_values(by=["user", "score"], ascending=False).groupby("user").head(k)
        return pairs
    scored = Parallel(n_jobs=num_threads)(delayed(predict_user)(model=model, 
                                                                        test_users=i, items=items, k=k) for i in inputs)
    return pd.concat(scored)
hand10ryo commented 2 years ago

Thank you very much! I just now added the method to predict topk items based on your code. I have invited you to collaborator, and could you please review it if possible? https://github.com/hand10ryo/PyTorchCML/pull/37