Implementation:Recommenders team Recommenders EmbDotBias Training
| Knowledge Sources | |
|---|---|
| Domains | Collaborative Filtering, Model Training, PyTorch |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
The Trainer class and predict_rating function provide the training loop and single-item prediction utilities for the EmbeddingDotBias collaborative filtering model.
Description
The Trainer class wraps a PyTorch model with an AdamW optimizer (betas=(0.9, 0.99), eps=1e-5) and MSELoss loss function. It handles automatic device placement to GPU when available. The train_epoch method runs a single epoch of forward/backward passes over batched data, computing MSE loss between predicted and actual ratings. The validate method evaluates the model on a validation set using torch.no_grad() for memory efficiency, returning None if the validation set is empty. The fit method orchestrates multi-epoch training with per-epoch logging of both training and validation losses. The standalone predict_rating function takes a trained model along with a user ID and item ID, converts them to embedding indices via the model's _get_idx method, and returns a single predicted rating score, with error handling that logs failures and returns None.
Usage
Use the Trainer class when training an EmbeddingDotBias model on user-item rating data. It provides a complete training pipeline that handles optimizer configuration, loss computation, and device management. Use predict_rating for generating individual rating predictions after training, such as in an API endpoint or interactive evaluation scenario.
Code Reference
Source Location
- Repository: Recommenders
- File: recommenders/models/embdotbias/training_utils.py
- Lines: 1-138
Signature
class Trainer:
def __init__(self, model, learning_rate=1e-3, weight_decay=0.01)
def train_epoch(self, train_dl)
def validate(self, valid_dl)
def fit(self, train_dl, valid_dl, n_epochs)
def predict_rating(model, user_id, item_id)
Import
from recommenders.models.embdotbias.training_utils import Trainer, predict_rating
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | torch.nn.Module | Yes | The PyTorch model to train (typically EmbeddingDotBias) |
| learning_rate | float | No | Learning rate for AdamW optimizer (default 1e-3) |
| weight_decay | float | No | Weight decay regularization for AdamW optimizer (default 0.01) |
| train_dl | DataLoader | Yes | Training data loader yielding (users_items, ratings) batches |
| valid_dl | DataLoader | Yes | Validation data loader yielding (users_items, ratings) batches |
| n_epochs | int | Yes | Number of training epochs |
| user_id (predict_rating) | str | Yes | The ID of the user for prediction |
| item_id (predict_rating) | str | Yes | The ID of the item for prediction |
Outputs
| Name | Type | Description |
|---|---|---|
| train_epoch return | float | Average training loss for the epoch |
| validate return | float or None | Average validation loss, or None if validation set is empty |
| predict_rating return | float or None | Predicted rating score, or None if an error occurs |
Usage Examples
Basic Usage
from recommenders.models.embdotbias.model import EmbeddingDotBias
from recommenders.models.embdotbias.training_utils import Trainer, predict_rating
# Build the model
classes = {"userID": user_ids, "itemID": item_ids}
model = EmbeddingDotBias.from_classes(n_factors=40, classes=classes, y_range=(1, 5))
# Create trainer and fit
trainer = Trainer(model, learning_rate=1e-3, weight_decay=0.01)
trainer.fit(train_dl, valid_dl, n_epochs=5)
# Predict a single rating
rating = predict_rating(model, user_id="user_42", item_id="item_101")