How to optimize a model

This page will explore different ways to optimize models in RePlay.

First we initialize a model

from replay.models import SLIM
model = SLIM()

If you just want to optimize a model with default settings, all you have to specify is a data to use for optimization.

model.optimize(train, val)

This will return a dict with the best parameters and set them.

If you are not pleased with the results you can continue optimizing by calling optimize with new_study=False, it will continue optimizing right where it stopped. Optuna study is stored as a model attribute. For example, you can see all trials with model.study.trials.

replay.models.base_rec.BaseRecommender.optimize(self, train, test, user_features=None, item_features=None, param_borders=None, criterion=<replay.metrics.ndcg.NDCG object>, k=10, budget=10, new_study=True)

Searches best parameters with optuna.

Parameters
  • train (DataFrame) – train data

  • test (DataFrame) – test data

  • user_features (Optional[DataFrame]) – user features

  • item_features (Optional[DataFrame]) – item features

  • param_borders (Optional[Dict[str, List[Any]]]) – a dictionary with search borders, where key is the parameter name and value is the range of possible values {param: [low, high]}. In case of categorical parameters it is all possible values: {cat_param: [cat_1, cat_2, cat_3]}.

  • criterion (Metric) – metric to use for optimization

  • k (int) – recommendation list length

  • budget (int) – number of points to try

  • new_study (bool) – keep searching with previous study or start a new study

Return type

Optional[Dict[str, Any]]

Returns

dictionary with best parameters

You can either use default borders or specify them yourself. A list of searchable parameters is specified in _search_space attribute.

model._search_space

{'beta': {'type': 'loguniform', 'args': [1e-06, 5]},
 'lambda_': {'type': 'loguniform', 'args': [1e-06, 2]}}

If you specify only one of the parameters, the other one will not be optimized.

model = SLIM(lambda_=1)
model.optimize(train, val, param_borders={'beta': [0.1, 1]})