Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Recommenders team Recommenders RLRMC Algorithm

From Leeroopedia


Knowledge Sources
Domains Matrix Completion, Riemannian Optimization, Collaborative Filtering
Last Updated 2026-02-10 00:00 GMT

Overview

The RLRMCalgorithm class implements the Riemannian Low-Rank Matrix Completion algorithm, a manifold-optimization approach to matrix completion for recommendation systems.

Description

The RLRMCalgorithm class provides a mathematically principled approach to matrix completion by formulating the problem as an optimization on a product manifold consisting of two Stiefel manifolds (for orthogonal factor matrices U1, U2) and a Symmetric Positive Definite manifold (for the scaling matrix B). The rating matrix is decomposed as U1 * B * U2^T.

Key implementation details:

  • Initialization: Supports both random initialization and SVD-based warm start via scipy's sparse SVD (svds), which can accelerate convergence.
  • Numba-accelerated operations: The core cost computation uses @njit (no-GIL, parallel) compiled functions for efficient sparse matrix operations in CSR format, avoiding Python overhead.
  • Riemannian optimization: Uses the custom ConjugateGradientMS solver with line search back-tracking to optimize on the manifold, computing both the objective function (sum of squared residuals plus L2 regularization on B) and Euclidean gradients (which Pymanopt converts to Riemannian gradients).
  • Statistics tracking: The fit_and_evaluate method supports per-iteration monitoring of training and validation RMSE through a callback mechanism.
  • Prediction: Reconstructs ratings as L * R^T where L = U1 * B and R = U2, with mean-centering handled by subtracting the training mean. Supports both vectorized and low-memory prediction modes.

Usage

Use this algorithm when you need a theoretically grounded matrix completion approach for collaborative filtering. It is especially useful when the underlying rating matrix is believed to be low-rank and when you want convergence guarantees that standard gradient descent methods do not provide. Suitable for medium-scale recommendation problems where Riemannian optimization overhead is acceptable.

Code Reference

Source Location

Signature

class RLRMCalgorithm(object):
    def __init__(
        self,
        rank,
        C,
        model_param,
        initialize_flag="random",
        max_time=1000,
        maxiter=100,
        seed=42,
    )

    def fit(self, RLRMCdata, verbosity=0, _evaluate=False)
    def fit_and_evaluate(self, RLRMCdata, verbosity=0)
    def predict(self, user_input, item_input, low_memory=False)

Import

from recommenders.models.rlrmc.RLRMCalgorithm import RLRMCalgorithm

I/O Contract

Inputs

Name Type Required Description
rank int Yes Rank of the final factorized model; must be a positive integer
C float Yes Regularization parameter controlling the L2 penalty on the scaling matrix B
model_param dict Yes Dictionary containing model parameters: 'num_row', 'num_col', and 'train_mean'
initialize_flag str No Initialization strategy: 'random' or 'svd'; default 'random'
max_time int No Maximum execution time in seconds; default 1000
maxiter int No Maximum number of optimization iterations; default 100
seed int No Random seed for reproducibility; default 42
RLRMCdata RLRMCdataset Yes (for fit) Dataset object containing train/validation CSR matrices and ID mappings
user_input list Yes (for predict) List of user IDs for which to predict ratings
item_input list Yes (for predict) List of item IDs for which to predict ratings (must match length of user_input)
low_memory bool No (for predict) If True, use a for-loop instead of matrix multiplication for prediction; default False

Outputs

Name Type Description
fit() None Trains the model in-place; stores factor matrices in self.L and self.R, and per-iteration statistics in self.stats
predict() numpy.ndarray Array of predicted ratings for the given user-item pairs

Usage Examples

Basic Usage

from recommenders.models.rlrmc.RLRMCalgorithm import RLRMCalgorithm

# Define model parameters
model_param = {
    "num_row": n_users,
    "num_col": n_items,
    "train_mean": train_mean_rating,
}

# Initialize the RLRMC algorithm
model = RLRMCalgorithm(
    rank=10,
    C=1e-3,
    model_param=model_param,
    initialize_flag="svd",
    max_time=600,
    maxiter=50,
)

# Fit the model with evaluation on validation data
model.fit_and_evaluate(rlrmc_dataset, verbosity=1)

# Predict ratings for specific user-item pairs
predictions = model.predict(
    user_input=[user1, user2, user3],
    item_input=[item1, item2, item3],
)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment