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

[BUG] Hyperparameter tuning for next item recommenders #643

Open
logrkn opened this issue Aug 15, 2024 · 3 comments
Open

[BUG] Hyperparameter tuning for next item recommenders #643

logrkn opened this issue Aug 15, 2024 · 3 comments

Comments

@logrkn
Copy link

logrkn commented Aug 15, 2024

I'm trying to do hyperparameter tuning for a next item recommender using yoochoose data (code below) but get the following error:

ValueError: model must be a NextItemRecommender but '<class 'cornac.hyperopt.RandomSearch'>' is provided

What is the correct approach for tuning next item recommenders?

import cornac
from cornac.data import Reader
from cornac.datasets import yoochoose
from cornac.eval_methods import NextItemEvaluation
from cornac.metrics import NDCG
from cornac.models import GRU4Rec
from cornac.hyperopt import Discrete, Continuous
from cornac.hyperopt import RandomSearch

buy_data = yoochoose.load_buy(reader=Reader(min_sequence_size=2, num_top_freq_item=30000))

item_set = set([tup[1] for tup in buy_data])

test_data = yoochoose.load_test(reader=Reader(min_sequence_size=2, item_set=item_set))

next_item_eval = NextItemEvaluation.from_splits(
    train_data=buy_data[:10000],
    test_data=test_data[:10000],  
    exclude_unknowns=True,
    verbose=False,
    fmt="SITJson",
)

ndcg20 = NDCG(20)

gr = GRU4Rec(layers=64, loss="bpr-max", n_sample=2048, n_epochs=5, seed=123, verbose=False)

rs_gr = RandomSearch(model=gr, 
                      space=[Discrete(name="layers", values=[64, 128, 256])], 
                      metric=ndcg20,
                      eval_method=next_item_eval,
                      n_trails=10)

cornac.Experiment(eval_method=next_item_eval, 
                  models=[rs_gr], 
                  metrics=[ndcg20], 
                  show_validation=False).run()
@tqtg
Copy link
Member

tqtg commented Aug 16, 2024

@lthoang is it something that we can try to address?

@lthoang
Copy link
Member

lthoang commented Aug 16, 2024

@tqtg Up to the current release 2.2.2, we haven't support hyperparameter tuning for NextItemRecommender model. We need another PR to support this feature.

@logrkn You can apply the following snippet to iterate through your candidate list of parameters:

import cornac
from cornac.data import Reader
from cornac.datasets import yoochoose
from cornac.eval_methods import NextItemEvaluation
from cornac.metrics import NDCG
from cornac.models import GRU4Rec
from cornac.hyperopt import Discrete, Continuous
from cornac.hyperopt import RandomSearch

buy_data = yoochoose.load_buy(reader=Reader(min_sequence_size=2, num_top_freq_item=30000))

item_set = set([tup[1] for tup in buy_data])

test_data = yoochoose.load_test(reader=Reader(min_sequence_size=2, item_set=item_set))

next_item_eval = NextItemEvaluation.from_splits(
    train_data=buy_data[:10000],
    test_data=test_data[:10000],  
    exclude_unknowns=True,
    verbose=False,
    fmt="SITJson",
)

models = [
    GRU4Rec(
        name="GRU4Rec_layer_{}".format(layer),
        layers=[layer], 
        loss="bpr-max",
        n_sample=2048,
        n_epochs=5,
        seed=123,
        verbose=False
    )
    for layer in [64, 128, 256]
]

cornac.Experiment(eval_method=next_item_eval, 
                  models=models, 
                  metrics=[NDCG(20)], 
                  show_validation=False).run()

@logrkn
Copy link
Author

logrkn commented Aug 16, 2024

Thank you, I'll try that. I would be great to get hyperparameter tuning included for NextItemRecommender models in the future though as ideally I'd like to test more combinations than in this simple example.

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

3 participants