Learn R Programming

reco (version 0.2.0.4)

WRMF: (Weighted) Regularized Matrix Facrtorization for collaborative filtering

Description

Creates matrix factorization model which could be solved with Alternating Least Squares (Weighted ALS for implicit feedback). For implicit feedback see (Hu, Koren, Volinsky)'2008 paper http://yifanhu.net/PUB/cf.pdf. For explicit feedback model is classic model for rating matrix decomposition with MSE error (without biases at the moment). These two algorithms are proven to work well in recommender systems.

Usage

WRMF

Format

R6Class object.

Usage

For usage details see Methods, Arguments and Examples sections.

  model = WRMF$new(rank = 10L, lambda = 0,
                  feedback = c("implicit", "explicit"),
                  init_stdv = 0.01,
                  n_threads = parallel::detectCores(),
                  non_negative = FALSE,
                  solver = c("conjugate_gradient", "cholesky"),
                  cg_steps = 3L,
                  components = NULL)
  model$fit_transform(x, n_iter = 5L, ...)
  model$predict(x, k, not_recommend = x, items_exclude = NULL, ...)
  model$components
  model$add_scorers(x_train, x_cv, specs = list("map10" = "map@10"), ...)
  model$remove_scorer(name)

Methods

$new(rank = 10L, lambda = 0, feedback = c("implicit", "explicit"), init_stdv = 0.01, n_threads = parallel::detectCores(), non_negative = FALSE, solver = c("conjugate_gradient", "cholesky"), cg_steps = 3L, components = NULL)

creates matrix factorization model model with rank latent factors. If components is provided then initialize item embeddings with its values.

$fit_transform(x, n_iter = 5L, ...)

fits model to an input user-item matrix. (preferably in "dgCMatrix" format). For implicit feedback x should be a confidence matrix which corresponds to 1 + alpha * r_ui in original paper. Usually r_ui corresponds to the number of interactions of user u and item i. For explicit feedback values in x represents ratings. Returns factor matrix for users of size n_users * rank

$predict(x, k, not_recommend = x, ...)

predicts top k item indices for users x. Additionally contains scores attribute - "score" values for each prediction. If model contains item ids (input matrix to fit_transform() had column-names then result additionally will have ids attribute - item ids which correspond to item indices. Users features x should be defined the same way as they were defined in training data - as sparse matrix of confidence values (implicit feedback) or ratings (explicit feedback). Column names (=item ids) should be in the same order as in the fit_transform().

$add_scorers(x_train, x_cv, specs = list("map10" = "map@10"), ...)

add a metric to watchlist. Metric will be evaluated after each ALS interation. At the moment following metrices are supported: "loss", "map@k", "ndcg@k", where k is some integer. For example map@10.

$remove_scorer(name)

remove a metric from watchlist

$components

item factors matrix of size rank * n_items

n_threads

numeric default number of threads to use during training and prediction (if OpenMP is available).

Arguments

model

A WRMF model.

x

An input sparse user-item matrix(of class dgCMatrix). For explicit feedback should consists of ratings. For implicit feedback all positive interactions should be filled with confidence values. Missed interactions should me zeros/empty. So for simple case case when confidence = 1 + alpha * x

x_train

An input user-item relevance matrix. Used during evaluation of map@k, ndcg@k Should have the same shape as corresponding confidence matrix x_cv. Values are used as "relevance" in ndgc calculation

x_cv

user-item matrix used for validation (ground-truth observations)

name

character - user-defined name of the scorer. For example "ndcg-scorer-1"

rank

integer - number of latent factors

lambda

numeric - regularization parameter

feedback

character - feedback type - one of c("implicit", "explicit")

solver

character - solver for "implicit feedback" problem. One of c("conjugate_gradient", "cholesky"). Usually approximate "conjugate_gradient" is significantly faster and solution is on par with exact "cholesky"

cg_steps

integer > 0 - max number of internal steps in conjugate gradient (if "conjugate_gradient" solver used). cg_steps = 3 by default. Controls precision of linear equation solution at the each ALS step. Usually no need to tune this parameter.

preprocess

function = identity() by default. User spectified function which will be applied to user-item interaction matrix before running matrix factorization (also applied in inference time before making predictions). For example we may want to normalize each row of user-item matrix to have 1 norm. Or apply log1p() to discount large counts. This essentially corresponds to the "confidence" function from (Hu, Koren, Volinsky)'2008 paper http://yifanhu.net/PUB/cf.pdf

n_threads

numeric default number of threads to use during training and prediction (if OpenMP is available).

not_recommend

sparse matrix or NULL - points which items should be excluided from recommendations for a user. By default it excludes previously seen/consumed items.

items_exclude

character = item ids or integer = item indices or NULL - items to exclude from recommendations for all users.

convergence_tol

numeric = -Inf defines early stopping strategy. We stop fitting when one of two following conditions will be satisfied: (a) we have used all iterations, or (b) loss_previous_iter / loss_current_iter - 1 < convergence_tol

init_stdv

numeric standart deviation for initialization of the initial latent matrices

...

other arguments. Not used at the moment

See Also