Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Recommenders team Recommenders NCF Init And Fit

From Leeroopedia


Knowledge Sources
Domains Recommender Systems, Deep Learning, Collaborative Filtering
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete tool for constructing and training a Neural Collaborative Filtering model provided by the recommenders library.

Description

The NCF class implements the Neural Collaborative Filtering framework from He et al. (WWW 2017) using TensorFlow 1.x. The __init__ method builds the computation graph for one of three model variants (GMF, MLP, or NeuMF), including user/item embedding layers, hidden layers, and the binary cross-entropy loss with an Adam optimizer. The fit method trains the model by iterating over epochs, consuming batches from the Dataset.train_loader generator which provides (user, item, label) tuples with on-the-fly negative sampling.

During initialization, the model validates the model_type parameter, creates the TensorFlow session with GPU memory growth enabled, and initializes all variables. The NCF layer size for NeuMF is computed as n_factors + layer_sizes[-1], reflecting the concatenation of the GMF and MLP output vectors.

During fitting, the model stores user/item ID mappings from the Dataset, then loops for n_epochs. Each epoch iterates over all training batches, converting original IDs to internal indices and computing the loss via session runs. Verbose logging reports per-epoch training loss and wall-clock time.

Usage

Import NCF after preparing the data with Dataset. Instantiate with the number of users and items from the dataset, select the model type, configure architecture hyperparameters (embedding dimension, layer sizes), and call fit(data) to train. After training, the model is ready for prediction via NCF.predict().

Code Reference

Source Location

  • Repository: recommenders
  • File: recommenders/models/ncf/ncf_singlenode.py
  • Lines: 27-91 (__init__), 369-414 (fit)

Signature

class NCF:
    def __init__(
        self,
        n_users,
        n_items,
        model_type="NeuMF",
        n_factors=8,
        layer_sizes=[16, 8, 4],
        n_epochs=50,
        batch_size=64,
        learning_rate=5e-3,
        verbose=1,
        seed=None,
    ):
    def fit(self, data):
        """Fit model with training data.

        Args:
            data (Dataset): Initialized Dataset from recommenders.models.ncf.dataset
        """

Import

from recommenders.models.ncf.ncf_singlenode import NCF

I/O Contract

Inputs (__init__)

Name Type Required Description
n_users int Yes Number of unique users in the dataset (from Dataset.n_users)
n_items int Yes Number of unique items in the dataset (from Dataset.n_items)
model_type str No Model variant: "GMF", "MLP", or "NeuMF". Defaults to "NeuMF"
n_factors int No Dimension of latent embedding space. Defaults to 8
layer_sizes list No Sizes of MLP hidden layers in a tower pattern. Defaults to [16, 8, 4]
n_epochs int No Number of training epochs. Defaults to 50
batch_size int No Mini-batch size for training. Defaults to 64
learning_rate float No Learning rate for the Adam optimizer. Defaults to 5e-3
verbose int No Logging frequency; prints training loss every verbose epochs. 0 for silent. Defaults to 1
seed int No Random seed for TensorFlow and NumPy reproducibility. Defaults to None

Inputs (fit)

Name Type Required Description
data Dataset Yes An initialized Dataset object from recommenders.models.ncf.dataset providing train_loader, user2id, item2id, id2user, and id2item

Outputs

Name Type Description
model NCF The NCF instance itself, with trained weights stored in the TensorFlow session. fit returns None; the model is mutated in place
model.user2id dict User ID to internal index mapping (set during fit)
model.item2id dict Item ID to internal index mapping (set during fit)
model.id2user dict Internal index to user ID mapping (set during fit)
model.id2item dict Internal index to item ID mapping (set during fit)

Usage Examples

Basic Usage

from recommenders.models.ncf.ncf_singlenode import NCF
from recommenders.models.ncf.dataset import Dataset

# Prepare dataset (see NCF_Dataset_Init)
data = Dataset(
    train_file="train.csv",
    test_file="test.csv",
    n_neg=4,
    n_neg_test=100,
    seed=42,
)

# Initialize and train NeuMF model
model = NCF(
    n_users=data.n_users,
    n_items=data.n_items,
    model_type="NeuMF",
    n_factors=8,
    layer_sizes=[16, 8, 4],
    n_epochs=50,
    batch_size=64,
    learning_rate=5e-3,
    seed=42,
)

model.fit(data)
# Output (per epoch):
# Epoch 1 [12.34s]: train_loss = 0.543210
# Epoch 2 [11.98s]: train_loss = 0.487654
# ...

GMF Variant

# Use GMF for a lightweight element-wise product model
model_gmf = NCF(
    n_users=data.n_users,
    n_items=data.n_items,
    model_type="GMF",
    n_factors=8,
    n_epochs=20,
    seed=42,
)
model_gmf.fit(data)

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

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