recpack.algorithms.BPRMF
- class recpack.algorithms.BPRMF(num_components: int = 100, lambda_h: float = 0.0, lambda_w: float = 0.0, batch_size: int = 1000, max_epochs: int = 20, learning_rate: float = 0.01, stopping_criterion: str = 'bpr', stop_early: bool = False, max_iter_no_change: int = 5, min_improvement: float = 0.01, seed: Optional[int] = None, save_best_to_file: bool = False, sample_size=None, keep_last: bool = False, predict_topK: Optional[int] = None, validation_sample_size: Optional[int] = None)
Implements Matrix Factorization by using the BPR-OPT objective and SGD optimization.
MF implementation using the BPR criterion as defined in Rendle, Steffen, et al. “BPR: Bayesian personalized ranking from implicit feedback.”
The BPR optimization criterion aims to construct a factorization that optimally ranks interesting items (interacted with previously) above uninteresting or unknown items for all users.
- Parameters
num_components (int, optional) – The size of the latent vectors for both users and items. defaults to 100
lambda_h (float, optional) – The regularization parameter for the item embedding, should be a value between 0 and 1. Defaults to 0.0
lambda_w (float, optional) – The regularization parameter for the user embedding, defaults to 0.0
batch_size (int, optional) – Size of the batches to use during gradient descent. Defaults to 1000.
max_epochs (int, optional) – The max amount of epochs to train the model, defaults to 20
learning_rate (float, optional) – The learning rate of the optimization procedure, defaults to 0.01
seed (int, optional) – Seed to fix random numbers, to make results reproducible, defaults to None
stopping_criterion (str, optional) – Which criterion to use optimise the parameters, a string which indicates the name of the stopping criterion. Which criterions are available can be found at
recpack.algorithms.stopping_criterion.StoppingCriterion.FUNCTIONS
. Defaults to ‘recall’stop_early (bool, optional) – If True, early stopping is enabled, and after
max_iter_no_change
iterations where improvement of loss function is belowmin_improvement
the optimisation is stopped, even if max_epochs is not reached. Defaults to Falsemax_iter_no_change (int, optional) – If early stopping is enabled, stop after this amount of iterations without change. Defaults to 5
min_improvement (float, optional) – If early stopping is enabled, no change is detected, if the improvement is below this value. Defaults to 0.01
save_best_to_file (bool, optional) – If True, the best model is saved to disk after fit.
sample_size (int, optional) – How many samples to take during training using bootstrap sampling. If None a sample is taken for each interaction, there is no guarantee that all interactions will be used though, since sampling happens with replacement. Defaults to None
keep_last (bool, optional) – Retain last model, rather than best (according to stopping criterion value on validation data), defaults to False
predict_topK (int, optional) – The topK recommendations to keep per row in the matrix. Use when the user x item output matrix would become too large for RAM. Defaults to None, which results in no filtering.
validation_sample_size (int, optional) – Amount of users that will be sampled to calculate validation loss and stopping criterion value. This reduces computation time during validation, such that training times are strongly reduced. If None, all nonzero users are used. Defaults to None.
Methods
fit
(X, validation_data)Fit the parameters of the model.
Get metadata routing of this object.
get_params
([deep])Get parameters for this estimator.
load
(filename)Load torch model from file.
predict
(X)Predicts scores, given the interactions in X
save
()Save the current model to disk.
set_fit_request
(*[, validation_data])Request metadata passed to the
fit
method.set_params
(**params)Set the parameters of the estimator.
Attributes
Name of the file at which save(self) will write the current best model.
Name of the object.
Name of the object's class.
- property filename
Name of the file at which save(self) will write the current best model.
- fit(X: Union[recpack.matrix.interaction_matrix.InteractionMatrix, scipy.sparse._csr.csr_matrix], validation_data: Tuple[Union[recpack.matrix.interaction_matrix.InteractionMatrix, scipy.sparse._csr.csr_matrix], Union[recpack.matrix.interaction_matrix.InteractionMatrix, scipy.sparse._csr.csr_matrix]]) recpack.algorithms.base.TorchMLAlgorithm
Fit the parameters of the model.
Interaction Matrix X will be used for training, the validation data tuple will be used to compute the evaluate scores.
This function provides the generic framework for training a PyTorch algorithm, such that each child class only needs to implement the
_transform_fit_input()
,_init_model()
,_train_epoch()
and_evaluate()
functions.The function will:
Transform input data to the expected types
Initialize the model using
_init_model()
Iterate for each epoch until max epochs, or when early stopping conditions are met.
Training step using
_train_epoch()
Evaluation step using
_evaluate()
Once the model has been fit, the best model is stored to disk, if specified during init.
- Returns
self, fitted algorithm
- Return type
- get_metadata_routing()
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns
routing – A
MetadataRequest
encapsulating routing information.- Return type
MetadataRequest
- get_params(deep=True)
Get parameters for this estimator.
- Parameters
deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns
params – Parameter names mapped to their values.
- Return type
dict
- property identifier
Name of the object.
Name is made by combining the class name with the parameters passed at construction time.
Constructed by recreating the initialisation call. Example:
Algorithm(param_1=value)
- load(filename)
Load torch model from file.
- Parameters
filename (str) – File to load the model from
- property name
Name of the object’s class.
- predict(X: Union[recpack.matrix.interaction_matrix.InteractionMatrix, scipy.sparse._csr.csr_matrix]) scipy.sparse._csr.csr_matrix
Predicts scores, given the interactions in X
Recommends items for each nonzero user in the X matrix.
This function is a wrapper around the
_predict()
method, and performs checks on in- and output data to guarantee proper computation.Checks that model is fitted correctly
checks the output using
_check_prediction()
function
- Parameters
X (Matrix) – interactions to predict from.
- Returns
The recommendation scores in a sparse matrix format.
- Return type
csr_matrix
- save()
Save the current model to disk.
filename of the file to save model in is defined by the
filename
property.
- set_fit_request(*, validation_data: Union[bool, None, str] = '$UNCHANGED$') recpack.algorithms.bprmf.BPRMF
Request metadata passed to the
fit
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed tofit
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it tofit
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.New in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters
validation_data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
validation_data
parameter infit
.- Returns
self – The updated object.
- Return type
object
- set_params(**params)
Set the parameters of the estimator.
- Parameters
params (dict) – Estimator parameters