Implementation:Recommenders team Recommenders BaseModel Fit
| 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:
- Data Loading — Uses
self.train_iterator.load_data_from_fileto stream training batches from the news and behaviors files. Each batch contains clicked title sequences, candidate title sequences (1 positive + npratio negatives), and labels. - Batch Training — For each batch, calls
self.train(batch_data_input), which extracts input features and labels via_get_input_label_from_iterand runsmodel.train_on_batch. - Loss Tracking — Accumulates per-step loss into
epoch_lossand displays running average loss via tqdm progress bar everyshow_stepsteps. - Step Limiting — If
step_limitis provided, breaks out of the batch loop early (useful for debugging). - Validation — After all training batches, calls
self.run_eval(valid_news_file, valid_behaviors_file)to compute validation metrics. - Test Evaluation — If test files are provided, also evaluates on the test set.
- 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
- Repository: recommenders-team/recommenders
- File:
recommenders/models/newsrec/models/base_model.py(lines 181-286)
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 viatrain_on_batchnumpy— For numerical operations in data loadingtqdm— For progress bar display during trainingtime— For measuring training and evaluation durations
Related Pages
Implements Principle
Requires Environment
- Environment:Recommenders_team_Recommenders_Python_Core_Dependencies
- Environment:Recommenders_team_Recommenders_GPU_CUDA_Environment