class recoder.model.Recoder(model: recoder.nn.FactorizationModel, num_items=None, num_users=None, optimizer_type='sgd', loss='mse', loss_params=None, use_cuda=False, user_based=True, item_based=True)[source]

Module to train/evaluate a recommendation recoder.nn.FactorizationModel.

  • model (FactorizationModel) – the factorization model to train.
  • num_items (int, optional) – the number of items to represent. If None, it will be computed from the first training dataset passed to train().
  • num_users (int, optional) – the number of users to represent. If not provided, it will be computed from the first training dataset passed to train().
  • optimizer_type (str, optional) – optimizer type (one of ‘sgd’, ‘adam’, ‘adagrad’, ‘rmsprop’).
  • loss (str or torch.nn.Module, optional) – loss function used to train the model. If loss is a str, it should be mse for recoder.losses.MSELoss, logistic for torch.nn.BCEWithLogitsLoss, or logloss for recoder.losses.MultinomialNLLLoss. If loss is a torch.nn.Module, then that Module will be used as a loss function and make sure that the loss reduction is a sum reduction and not an elementwise mean.
  • loss_params (dict, optional) – loss function extra params based on loss module if loss is a str. Ignored if loss is a torch.nn.Module.
  • use_cuda (bool, optional) – use GPU when training/evaluating the model.
  • user_based (bool, optional) – If your model is based on users or not. If True, an exception will will be raised when there are inconsistencies between the users represented in the model and the users in the training datasets.
  • item_based (bool, optional) – If your model is based on items or not. If True, an exception will will be raised when there are inconsistencies between the items represented in the model and the items in the training datasets.

Initializes the model from a pre-trained model

Parameters:model_file (str) – the pre-trained model file path

Saves the model state in the path starting with model_checkpoint_prefix and appending it with the model current training epoch

Parameters:model_checkpoint_prefix (str) – the model save path prefix
Returns:the model state file path
train(train_dataset, val_dataset=None, lr=0.001, weight_decay=0, num_epochs=1, iters_per_epoch=None, batch_size=64, lr_milestones=None, negative_sampling=False, num_sampling_users=0, num_data_workers=0, model_checkpoint_prefix=None, checkpoint_freq=0, eval_freq=0, eval_num_recommendations=None, eval_num_users=None, metrics=None, eval_batch_size=None)[source]

Trains the model

  • train_dataset (RecommendationDataset) – train dataset.
  • val_dataset (RecommendationDataset, optional) – validation dataset.
  • lr (float, optional) – learning rate.
  • weight_decay (float, optional) – weight decay (L2 normalization).
  • num_epochs (int, optional) – number of epochs to train the model.
  • iters_per_epoch (int, optional) – number of training iterations per training epoch. If None, one epoch is full number of training samples in the dataset
  • batch_size (int, optional) – batch size
  • lr_milestones (list, optional) – optimizer learning rate epochs milestones (0.1 decay).
  • negative_sampling (bool, optional) – whether to apply mini-batch based negative sampling or not.
  • num_sampling_users (int, optional) – number of users to consider for sampling items. This is useful for increasing the number of negative samples in mini-batch based negative sampling while keeping the batch-size small. If 0, then num_sampling_users will be equal to batch_size.
  • num_data_workers (int, optional) – number of data workers to use for building the mini-batches.
  • checkpoint_freq (int, optional) – epochs frequency of saving a checkpoint of the model
  • model_checkpoint_prefix (str, optional) – model checkpoint save path prefix
  • eval_freq (int, optional) – epochs frequency of doing an evaluation
  • eval_num_recommendations (int, optional) – num of recommendations to generate on evaluation
  • eval_num_users (int, optional) – number of users from the validation dataset to use for evaluation. If None, all users in the validation dataset are used for evaluation.
  • metrics (list[Metric], optional) – list of Metric used to evaluate the model
  • eval_batch_size (int, optional) – the size of the evaluation batch
predict(users_interactions, return_input=False)[source]

Predicts the user interactions with all items

  • users_interactions (UsersInteractions) – A batch of users’ history consisting of list of Interaction
  • return_input (bool, optional) – whether to return the dense input batch

if return_input is True a tuple of the predictions and the input batch is returned, otherwise only the predictions are returned

recommend(users_interactions, num_recommendations)[source]

Generate list of recommendations for each user in users_hist.

  • users_interactions (UsersInteractions) – list of users interactions.
  • num_recommendations (int) – number of recommendations to generate.

list of recommended items for each user in users_interactions.

Return type:


evaluate(eval_dataset, num_recommendations, metrics, batch_size=1, num_users=None)[source]

Evaluates the current model given an evaluation dataset.

  • eval_dataset (RecommendationDataset) – evaluation dataset
  • num_recommendations (int) – number of top recommendations to consider.
  • metrics (list) – list of Metric to use for evaluation.
  • batch_size (int, optional) – batch size of computations.