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 BaseModel Fit

From Leeroopedia


Knowledge Sources
Domains News Recommendation, Model Training
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete tool for training a neural news recommendation model through epoch-based training with per-epoch validation, negative sampling, and progress reporting.

Description

BaseModel.fit implements the full training loop for all neural news recommendation models that inherit from BaseModel. The method performs the following for each epoch:

  1. Data Loading — Uses self.train_iterator.load_data_from_file to stream training batches from the news and behaviors files. Each batch contains clicked title sequences, candidate title sequences (1 positive + npratio negatives), and labels.
  2. Batch Training — For each batch, calls self.train(batch_data_input), which extracts input features and labels via _get_input_label_from_iter and runs model.train_on_batch.
  3. Loss Tracking — Accumulates per-step loss into epoch_loss and displays running average loss via tqdm progress bar every show_step steps.
  4. Step Limiting — If step_limit is provided, breaks out of the batch loop early (useful for debugging).
  5. Validation — After all training batches, calls self.run_eval(valid_news_file, valid_behaviors_file) to compute validation metrics.
  6. Test Evaluation — If test files are provided, also evaluates on the test set.
  7. Reporting — Prints training loss, validation metrics, and timing information for each epoch.

The method returns self, enabling method chaining.

Usage

Call fit() on an initialized NRMSModel (or other news recommendation model) to begin training. The method handles the entire training lifecycle including validation. After fit() returns, the model is ready for final evaluation or prediction export.

Code Reference

Source Location

Signature

def fit(
    self,
    train_news_file: str,
    train_behaviors_file: str,
    valid_news_file: str,
    valid_behaviors_file: str,
    test_news_file: str = None,
    test_behaviors_file: str = None,
    step_limit: int = None,
) -> "BaseModel":
    """Fit the model with training data. Evaluate on validation data per epoch.

    Args:
        train_news_file (str): Path to training news.tsv file.
        train_behaviors_file (str): Path to training behaviors.tsv file.
        valid_news_file (str): Path to validation news.tsv file.
        valid_behaviors_file (str): Path to validation behaviors.tsv file.
        test_news_file (str): Optional path to test news.tsv file.
        test_behaviors_file (str): Optional path to test behaviors.tsv file.
        step_limit (int): Optional maximum number of training steps per epoch.

    Returns:
        self: The trained model instance.
    """

Import

# Accessed via the NRMSModel class (inherits from BaseModel)
from recommenders.models.newsrec.models.nrms import NRMSModel
from recommenders.models.newsrec.io.mind_iterator import MINDIterator

model = NRMSModel(hparams, MINDIterator, seed=42)
# model.fit(...) is inherited from BaseModel

I/O Contract

Parameter Type Description
train_news_file str Path to the training news.tsv file
train_behaviors_file str Path to the training behaviors.tsv file
valid_news_file str Path to the validation news.tsv file
valid_behaviors_file str Path to the validation behaviors.tsv file
test_news_file str or None Optional path to test news.tsv file
test_behaviors_file str or None Optional path to test behaviors.tsv file
step_limit int or None Optional maximum training steps per epoch for early stopping within an epoch
Return Type Description
self BaseModel The trained model instance (enables method chaining)

Side Effects

Effect Description
Model weights updated The self.model weights are updated via backpropagation each step
Console output Prints per-epoch training loss, validation metrics, and timing
Progress bar Displays tqdm progress bar during each epoch's training

Usage Examples

import os

train_news_file = os.path.join(data_path, "train", "news.tsv")
train_behaviors_file = os.path.join(data_path, "train", "behaviors.tsv")
valid_news_file = os.path.join(data_path, "valid", "news.tsv")
valid_behaviors_file = os.path.join(data_path, "valid", "behaviors.tsv")

# Full training run
model.fit(
    train_news_file,
    train_behaviors_file,
    valid_news_file,
    valid_behaviors_file,
)

# Quick debug run with step limit
model.fit(
    train_news_file,
    train_behaviors_file,
    valid_news_file,
    valid_behaviors_file,
    step_limit=10,
)

# Training with test set evaluation
model.fit(
    train_news_file,
    train_behaviors_file,
    valid_news_file,
    valid_behaviors_file,
    test_news_file=os.path.join(data_path, "test", "news.tsv"),
    test_behaviors_file=os.path.join(data_path, "test", "behaviors.tsv"),
)

Dependencies

  • tensorflow — For model training via train_on_batch
  • numpy — For numerical operations in data loading
  • tqdm — For progress bar display during training
  • time — For measuring training and evaluation durations

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

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