recpack.algorithms.MultVAE
- class recpack.algorithms.MultVAE(batch_size: int = 500, max_epochs: int = 200, learning_rate: float = 0.0001, seed: Optional[int] = None, dim_bottleneck_layer: int = 200, dim_hidden_layer: int = 600, max_beta: float = 0.2, anneal_steps: int = 200000, dropout: float = 0.5, stopping_criterion: str = 'ndcg', stop_early: bool = False, max_iter_no_change: int = 5, min_improvement: int = 0.01, save_best_to_file=False, keep_last: bool = False, predict_topK: Optional[int] = None, validation_sample_size: Optional[int] = None)
MultVAE Algorithm as first discussed in ‘Variational Autoencoders for Collaborative Filtering’, D. Liang et al. @ KDD2018.
An Auto Encoder neural network’s goal is to reconstruct the original matrix after being passed through a bottleneck layer and several hidden layers. This method assumes a Multinomial likelihood for the data distribution. This rewards the model for putting probability mass on the non-zero entries in x_u. But the model has a limited budget of probability mass since π(z_u) must sum to 1; the items must compete for a limited budget.
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
learning_rate ([type], optional) – Learning rate, defaults to 1e-4
seed (int, optional) – Random seed for Torch, provided for reproducibility, defaults to None.
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
max_beta (float, optional) – Regularization parameter, annealed over
anneal_steps
until it reaches max_beta, defaults to 0.2anneal_steps (int, optional) – Number of steps to anneal beta to
max_beta
, defaults to 200000dropout (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 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.
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