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 EmbeddingDotBias Model

From Leeroopedia


Knowledge Sources
Domains Collaborative Filtering, Matrix Factorization, PyTorch
Last Updated 2026-02-10 00:00 GMT

Overview

The EmbeddingDotBias class implements a dot-product collaborative filtering model in PyTorch that predicts ratings using learned user and item embeddings combined with bias terms.

Description

The EmbeddingDotBias model is a fundamental matrix factorization approach for collaborative filtering. It creates four embedding layers: user weights, item weights, user biases, and item biases, all initialized with truncated normal distributions (std=0.01). The forward pass computes the prediction as dot(user_embedding, item_embedding) + user_bias + item_bias, optionally mapping the result through a sigmoid function scaled to a specified output range (y_range). A from_classes factory method allows building the model by inferring user and item counts from a class dictionary. Helper methods bias() and weight() allow extracting learned embeddings and biases for specific users or items by their original IDs, converting them through an internal _get_idx mapping that translates entity IDs to embedding matrix indices.

Usage

Use this model when building a collaborative filtering recommendation system that needs to predict explicit ratings from user-item interactions. It serves as a strong baseline for rating prediction tasks and is suitable for datasets where user and item IDs are known. The model supports both direct instantiation with known user/item counts and a factory pattern via from_classes for inferring counts from class dictionaries.

Code Reference

Source Location

Signature

class EmbeddingDotBias(Module):
    def __init__(self, n_factors, n_users, n_items, y_range=None)
    def forward(self, x)
    @classmethod
    def from_classes(cls, n_factors, classes, user=None, item=None, y_range=None)
    def _get_idx(self, entity_ids, is_item=True)
    def bias(self, entity_ids, is_item=True)
    def weight(self, entity_ids, is_item=True)

Import

from recommenders.models.embdotbias.model import EmbeddingDotBias

I/O Contract

Inputs

Name Type Required Description
n_factors int Yes Number of latent factors for user and item embeddings
n_users int Yes Total number of users in the dataset
n_items int Yes Total number of items in the dataset
y_range tuple No Output normalization range as (min, max); if None, raw scores are returned
x (forward) torch.Tensor Yes Tensor of shape (batch_size, 2) with user indices in column 0 and item indices in column 1
classes (from_classes) dict Yes Dictionary mapping entity names to lists of IDs for inferring user/item counts
entity_ids (bias/weight) list Yes List of user or item IDs for embedding/bias lookup
is_item bool No If True, fetch item embeddings/biases; if False, fetch user embeddings/biases (default True)

Outputs

Name Type Description
forward return torch.Tensor Predicted ratings for each user-item pair in the batch
from_classes return EmbeddingDotBias Instantiated model with classes attribute set
bias return torch.Tensor Bias values for the specified entities
weight return torch.Tensor Embedding weight vectors for the specified entities

Usage Examples

Basic Usage

from recommenders.models.embdotbias.model import EmbeddingDotBias

# Direct instantiation
model = EmbeddingDotBias(n_factors=40, n_users=1000, n_items=500, y_range=(1, 5))

# Using factory method with class dictionaries
classes = {"userID": [1, 2, 3, 4, 5], "itemID": [10, 20, 30]}
model = EmbeddingDotBias.from_classes(n_factors=40, classes=classes, y_range=(1, 5))

# Forward pass with a batch of user-item pairs
import torch
x = torch.tensor([[0, 1], [2, 0]])  # user indices, item indices
predictions = model(x)

# Extract learned biases and weights for specific items
item_biases = model.bias([10, 20], is_item=True)
item_weights = model.weight([10, 20], is_item=True)

Related Pages

Page Connections

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