recpack.algorithms.RecVAE
- class recpack.algorithms.RecVAE(batch_size: int = 500, max_epochs: int = 200, learning_rate: float = 0.0005, n_enc_epochs: int = 3, n_dec_epochs: int = 1, seed: Optional[int] = None, dim_bottleneck_layer: int = 200, dim_hidden_layer: int = 600, gamma: Optional[float] = 0.005, beta: Optional[float] = None, dropout: float = 0.5, stopping_criterion: str = 'ndcg', stop_early: bool = False, max_iter_no_change: int = 5, min_improvement: float = 0.0, save_best_to_file: bool = False, keep_last: bool = False, predict_topK: Optional[int] = None, validation_sample_size: Optional[int] = None)
RecVAE Algorithm as first discussed in ‘RecVAE: a New Variational Autoencoder for Top-NRecommendations with Implicit Feedback’, I. Shenbin et al. @ WSDM2020.
The RecVAE model is an extension of
recpack.algorithms.MultVAE
. With the biggest changes being: alternating updates to decoder and encoder during training and a novel composite prior distribution.Default values for parameters were taken from the paper.
- Parameters
batch_size (int, optional) – Batch size for SGD, defaults to 500
max_epochs (int, optional) – Maximum number of epochs (iterations), defaults to 200
n_enc_epochs (int) – The training happens alternating, in every epoch this amount of optimizations happen for the encoder network
n_dec_epochs (int) – The number of times to optimize the decoder network each epoch.
seed (int, optional) – Random seed for Torch, provided for reproducibility, defaults to None.
learning_rate (float, optional) – Learning rate, defaults to 1e-4
dim_bottleneck_layer (int, optional) – Size of the latent representation, defaults to 200
dim_hidden_layer (int, optional) – Dimension of the hidden layer, defaults to 600
gamma (float, optional) – Parameter defining regularization of the KL loss together with the norm of the output, defaults to 1
beta (float, optional) – Regularization parameter of the KL loss, only used if gamma = None, defaults to None
dropout (float, optional) – Dropout rate to apply at the inputs, defaults to 0.5
stopping_criterion (str, optional) – Used to identify the best model computed thus far. The string indicates the name of the stopping criterion. Which criterions are available can be found at
recpack.algorithms.stopping_criterion.StoppingCriterion.FUNCTIONS
. Defaults to ‘ndcg’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 – 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. Defaults to False.
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_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_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_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_params(**params)
Set the parameters of the estimator.
- Parameters
params (dict) – Estimator parameters