Implementation:Recommenders team Recommenders NRMSModel Init
Appearance
| Knowledge Sources | |
|---|---|
| Domains | News Recommendation, Deep Learning, Self-Attention |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for instantiating the NRMS (Neural News Recommendation with Multi-Head Self-Attention) model, including loading pre-trained word embeddings, building the news encoder, user encoder, and scorer.
Description
NRMSModel.__init__ initializes the full NRMS model through a two-phase process:
Phase 1 (NRMSModel-specific):
- Loads the pre-trained word embedding matrix from the numpy file specified in
hparams.wordEmb_filevia_init_embedding. - Stores the embedding as
self.word2vec_embedding.
Phase 2 (BaseModel.__init__):
- Sets the random seed for TensorFlow and NumPy.
- Creates train and test iterators from the provided
iterator_creator(typicallyMINDIterator). - Configures the TensorFlow session with GPU memory growth.
- Calls
_build_graph, which in turn calls_build_nrmsto construct:- A news encoder (word embedding -> dropout -> multi-head self-attention -> dropout -> additive attention).
- A user encoder (TimeDistributed news encoder -> multi-head self-attention -> additive attention).
- A training model that scores
npratio + 1candidates via dot product + softmax. - A scorer model that scores a single candidate via dot product + sigmoid.
- Compiles the training model with the configured loss function and optimizer.
Usage
Instantiate NRMSModel after preparing hyperparameters with prepare_hparams. The model is then ready for training via fit() and evaluation via run_eval().
Code Reference
Source Location
- Repository: recommenders-team/recommenders
- File (NRMSModel.__init__):
recommenders/models/newsrec/models/nrms.py(lines 27-48) - File (BaseModel.__init__):
recommenders/models/newsrec/models/base_model.py(lines 29-77)
Signature
class NRMSModel(BaseModel):
def __init__(
self,
hparams: HParams,
iterator_creator: type,
seed: int = None,
):
"""Initialization steps for NRMS.
Args:
hparams (HParams): Global hyper-parameters (head_num, head_dim, etc.).
iterator_creator (type): Data loader class (e.g., MINDIterator).
seed (int): Random seed for reproducibility.
"""
Import
from recommenders.models.newsrec.models.nrms import NRMSModel
from recommenders.models.newsrec.io.mind_iterator import MINDIterator
I/O Contract
| Parameter | Type | Description |
|---|---|---|
hparams |
HParams |
Validated hyperparameter object containing model architecture and training settings |
iterator_creator |
type |
Data iterator class (e.g., MINDIterator) that will be instantiated for train/test
|
seed |
int or None |
Random seed for reproducible initialization |
| Attribute | Type | Description |
|---|---|---|
self.model |
keras.Model |
Training model (multi-candidate scoring with softmax) |
self.scorer |
keras.Model |
Inference model (single-candidate scoring with sigmoid) |
self.newsencoder |
keras.Model |
News encoder submodel |
self.userencoder |
keras.Model |
User encoder submodel |
self.word2vec_embedding |
numpy.ndarray |
Pre-trained word embedding matrix |
self.train_iterator |
MINDIterator |
Training data iterator (with negative sampling) |
self.test_iterator |
MINDIterator |
Test/validation data iterator (no negative sampling) |
Usage Examples
from recommenders.models.newsrec.models.nrms import NRMSModel
from recommenders.models.newsrec.io.mind_iterator import MINDIterator
from recommenders.models.newsrec.newsrec_utils import prepare_hparams
# Prepare hyperparameters
hparams = prepare_hparams(
yaml_file,
wordEmb_file=wordEmb_file,
wordDict_file=wordDict_file,
userDict_file=userDict_file,
batch_size=32,
epochs=5,
head_num=20,
head_dim=20,
learning_rate=0.0001,
npratio=4,
title_size=30,
his_size=50,
data_format="news",
support_quick_scoring=True,
)
# Initialize the NRMS model
model = NRMSModel(hparams, MINDIterator, seed=42)
# The model is now ready for training and evaluation
# model.fit(train_news, train_behaviors, valid_news, valid_behaviors)
Dependencies
tensorflow— Deep learning framework (TF 1.x compatibility mode)tensorflow.keras— Keras API for building neural network layers and modelsnumpy— For loading pre-trained word embeddings and numerical operationsrecommenders.models.newsrec.models.base_model.BaseModel— Parent class providing training, evaluation, and session managementrecommenders.models.newsrec.models.layers.AttLayer2— Additive attention layerrecommenders.models.newsrec.models.layers.SelfAttention— Multi-head self-attention layer
Related Pages
Implements Principle
Requires Environment
- Environment:Recommenders_team_Recommenders_Python_Core_Dependencies
- Environment:Recommenders_team_Recommenders_GPU_CUDA_Environment
Uses Heuristic
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment