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_dataset, test_dataset, param_borders=None, criterion=<class 'replay.metrics.ndcg.NDCG'>, k=10, budget=10, new_study=True)
Searches the best parameters with optuna.
- Parameters
train_dataset (Dataset) – train data
test_dataset (Dataset) – test data
param_borders (Optional[dict[str, list]]) – 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
- Returns
dictionary with best parameters
- Return type
Optional[dict]
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]})