Implementation:Recommenders team Recommenders RBM
| Knowledge Sources | |
|---|---|
| Domains | Collaborative Filtering, Generative Models, Deep Learning |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
The RBM class implements a multinomial Restricted Boltzmann Machine for collaborative filtering using TensorFlow 1.x, learning user preferences from rating data through contrastive divergence training.
Description
The RBM (Restricted Boltzmann Machine) class provides a complete implementation of a generative model for recommendation based on the paper by Salakhutdinov, Mnih, and Hinton. The model uses multinomial visible units (instead of one-hot-encoded units) to represent discrete ratings, with a weight matrix connecting visible units (items) to hidden units (latent features).
Training follows the Contrastive Divergence (CD-k) algorithm:
- Gibbs Sampling: Alternates between sampling hidden units from visible units (forward pass via sigmoid activation and binomial sampling) and sampling visible units from hidden units (backward pass via multinomial distribution sampling).
- Adaptive Sampling Protocol: The number of Gibbs sampling steps (k) increases over training epochs according to a configurable protocol, improving estimation quality as optimization converges.
- Free Energy Minimization: Weights and biases are updated by minimizing the difference between the free energy clamped on the data and the model free energy after k sampling steps.
Key features include dropout regularization on hidden units, minibatch training via TensorFlow data pipelines, GPU memory management, optional RMSE metrics tracking during training, and model save/load via TensorFlow checkpoints. Prediction reconstructs ratings by propagating observed items through the hidden layer and sampling from the learned joint distribution.
Usage
Use this class when building a collaborative filtering recommender system that leverages generative modeling. It is particularly suited for explicit rating prediction tasks where the rating scale is discrete (e.g., 1-5 stars). The RBM approach is useful when you want to model the full joint distribution of user-item ratings rather than just point predictions.
Code Reference
Source Location
- Repository: Recommenders
- File: recommenders/models/rbm/rbm.py
- Lines: 1-736
Signature
class RBM:
def __init__(
self,
possible_ratings,
visible_units,
hidden_units=500,
keep_prob=0.7,
init_stdv=0.1,
learning_rate=0.004,
minibatch_size=100,
training_epoch=20,
display_epoch=10,
sampling_protocol=[50, 70, 80, 90, 100],
debug=False,
with_metrics=False,
seed=42,
)
def binomial_sampling(self, pr)
def multinomial_sampling(self, pr)
def multinomial_distribution(self, phi)
def free_energy(self, x)
def sample_hidden_units(self, vv)
def sample_visible_units(self, h)
def gibbs_sampling(self)
def fit(self, xtr)
def predict(self, x)
def recommend_k_items(self, x, top_k=10, remove_seen=True)
def save(self, file_path="./rbm_model.ckpt")
def load(self, file_path="./rbm_model.ckpt")
Import
from recommenders.models.rbm.rbm import RBM
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| possible_ratings | list of float | Yes | Sorted list of all unique ratings in the dataset (e.g., [1, 2, 3, 4, 5]) |
| visible_units | int | Yes | Number of visible units, equal to the number of items in the dataset |
| hidden_units | int | No | Number of hidden units (latent features); default 500 |
| keep_prob | float | No | Keep probability for dropout regularization; default 0.7 |
| init_stdv | float | No | Standard deviation for weight matrix initialization; default 0.1 |
| learning_rate | float | No | Learning rate for the Adam optimizer; default 0.004 |
| minibatch_size | int | No | Size of minibatches for training; default 100 |
| training_epoch | int | No | Number of training epochs; default 20 |
| display_epoch | int | No | Interval for displaying RMSE during training; default 10 |
| sampling_protocol | list of int | No | Percentages of total epochs at which Gibbs sampling steps increment; default [50, 70, 80, 90, 100] |
| debug | bool | No | Enable debug output; default False |
| with_metrics | bool | No | Compute RMSE during training; default False |
| seed | int | No | Random seed for reproducibility; default 42 |
Outputs
| Name | Type | Description |
|---|---|---|
| fit() | None | Trains the model in-place; stores training RMSE history in self.rmse_train |
| predict(x) | numpy.ndarray | Returns the inferred ratings matrix for all users and items |
| recommend_k_items(x) | numpy.ndarray | Returns a sparse matrix containing top-k items ordered by relevancy score (rating * probability) |
Usage Examples
Basic Usage
from recommenders.models.rbm.rbm import RBM
# Define the possible ratings and number of items
possible_ratings = [1, 2, 3, 4, 5]
n_items = 1000
# Initialize the RBM model
model = RBM(
possible_ratings=possible_ratings,
visible_units=n_items,
hidden_units=300,
training_epoch=30,
minibatch_size=128,
with_metrics=True,
)
# Train the model on the user-item affinity matrix
# xtr is a numpy array of shape (n_users, n_items) with 0 for unrated items
model.fit(xtr)
# Predict ratings for all users
predicted_ratings = model.predict(xtr)
# Get top-10 recommendations, excluding already-seen items
top_k_scores = model.recommend_k_items(xtr, top_k=10, remove_seen=True)
# Save and load the model
model.save("./my_rbm_model.ckpt")
model.load("./my_rbm_model.ckpt")