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

From Leeroopedia
Revision as of 16:29, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Recommenders_team_Recommenders_NAML_Model.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains News Recommendation, Deep Learning, Multi-View Learning
Last Updated 2026-02-10 00:00 GMT

Overview

The NAMLModel implements the NAML (Neural News Recommendation with Attentive Multi-View Learning) model, which leverages multiple views of news articles -- title, body, category, and subcategory -- combined through attentive pooling for news recommendation.

Description

NAMLModel extends BaseModel to implement the multi-view news recommendation architecture from Wu et al. (IJCAI 2019). The defining characteristic of NAML is its multi-view news encoder that processes four distinct aspects of each news article and fuses them through attention.

The news encoder is composed of four specialized sub-encoders:

  • Title encoder: Takes title word indices through a pretrained word embedding layer, applies dropout, processes with a 1D CNN (configurable filters, window size, and activation), applies more dropout, then uses AttLayer2 (additive attention) to aggregate into a single vector, reshaped to (1, filter_num).
  • Body encoder: Identical architecture to the title encoder but operating on article body (abstract) text with its own body_size parameter.
  • Vertical (category) encoder: Passes the category index through an embedding layer (vert_num x vert_emb_dim), then a dense layer to project to filter_num dimensions, reshaped to (1, filter_num).
  • Sub-vertical (subcategory) encoder: Same architecture as the vert encoder but with separate subcategory embeddings (subvert_num x subvert_emb_dim).

The four sub-encoder outputs are concatenated along the view axis (producing a 4 x filter_num matrix) and then fused via AttLayer2 attention, which learns to weight the importance of each view dynamically for each news article.

The user encoder applies AttLayer2 attention over the sequence of encoded clicked news articles (wrapped with TimeDistributed) to produce a user representation that weights different historical clicks by their relevance.

The full model concatenates all input features (title, body, vert, subvert) for both clicked and candidate news. Click probability is computed via dot product between news and user representations, with softmax during training and sigmoid during single-item inference.

Usage

Use NAMLModel when building a news recommendation system with access to rich article metadata beyond just titles. NAML is particularly effective when news articles have meaningful category/subcategory taxonomies and substantive body text. This model specifically requires the MINDAllIterator for data loading, since it processes the full set of article features. It is best suited for scenarios where leveraging multiple content signals leads to better news understanding.

Code Reference

Source Location

Signature

class NAMLModel(BaseModel):
    def __init__(self, hparams, iterator_creator, seed=None)
    def _get_input_label_from_iter(self, batch_data)
    def _get_user_feature_from_iter(self, batch_data)
    def _get_news_feature_from_iter(self, batch_data)
    def _build_graph(self)
    def _build_userencoder(self, newsencoder)
    def _build_newsencoder(self, embedding_layer)
    def _build_titleencoder(self, embedding_layer)
    def _build_bodyencoder(self, embedding_layer)
    def _build_vertencoder(self)
    def _build_subvertencoder(self)
    def _build_naml(self)

Import

from recommenders.models.newsrec.models.naml import NAMLModel

I/O Contract

Inputs

Name Type Required Description
hparams object Yes Global hyper-parameters including word_emb_dim, title_size, body_size, his_size, filter_num, window_size, cnn_activation, dense_activation, dropout, attention_hidden_dim, vert_num, vert_emb_dim, subvert_num, subvert_emb_dim, npratio, and wordEmb_file.
iterator_creator object Yes Data iterator creator class. Must be MINDAllIterator to provide body, vert, and subvert features.
seed int No Random seed for reproducibility of weight initialization.
batch_data dict Yes (at runtime) Dictionary containing "clicked_title_batch", "clicked_ab_batch", "clicked_vert_batch", "clicked_subvert_batch", "candidate_title_batch", "candidate_ab_batch", "candidate_vert_batch", "candidate_subvert_batch", and "labels".

Outputs

Name Type Description
model keras.Model Training model accepting 8 inputs (clicked and candidate title/body/vert/subvert) and outputting softmax probabilities.
scorer keras.Model Inference model accepting clicked history inputs plus single candidate inputs and outputting a sigmoid score.
self.newsencoder keras.Model Multi-view news encoder sub-model for standalone news encoding during inference.
self.userencoder keras.Model User encoder sub-model for standalone user encoding during inference.

Usage Examples

Basic Usage

from recommenders.models.newsrec.models.naml import NAMLModel
from recommenders.models.newsrec.io.mind_all_iterator import MINDAllIterator
from recommenders.models.newsrec.newsrec_utils import prepare_hparams

# Prepare hyperparameters from a YAML config
hparams = prepare_hparams(
    yaml_file,
    wordEmb_file=word_embedding_path,
    wordDict_file=word_dict_path,
    userDict_file=user_dict_path,
    vertDict_file=vert_dict_path,
    subvertDict_file=subvert_dict_path,
    title_size=30,
    body_size=50,
    vert_num=18,
    vert_emb_dim=100,
    subvert_num=300,
    subvert_emb_dim=100,
    npratio=4,
    his_size=50,
    batch_size=32,
)

# Create the NAML model (requires MINDAllIterator for multi-view features)
model = NAMLModel(hparams, MINDAllIterator, seed=42)

# Train the model
model.fit(train_news_file, train_behaviors_file, valid_news_file, valid_behaviors_file)

# Evaluate on test data
results = model.run_eval(test_news_file, test_behaviors_file)
print(results)  # e.g., {"group_auc": 0.66, "ndcg@5": 0.36, ...}

Related Pages

Page Connections

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