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

From Leeroopedia


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

Overview

The NPAModel implements the NPA (Neural News Recommendation with Personalized Attention) model, which personalizes both news encoding and user representation by using user-specific attention mechanisms driven by user ID embeddings.

Description

NPAModel extends BaseModel to implement the personalized attention architecture from Wu et al. (KDD 2019). The key innovation of NPA is that the same news article can receive different representations for different users, because attention weights in both the news encoder and user encoder are conditioned on the user's learned embedding.

The news encoder takes a concatenated input of title word indices and the user index. The user index is passed through a shared user embedding layer and projected through a dense layer to produce a user preference query vector. Title words go through a pretrained word embedding layer, dropout, and a 1D CNN (with configurable filters, window size, and activation), followed by more dropout. The CNN output is then aggregated via PersonalizedAttentivePooling, which computes attention weights using the user preference query -- this means different users attend to different words in the same news title, producing personalized news representations.

The user encoder takes the user's clicked news history (encoded via TimeDistributed wrapping of the news encoder, with user IDs replicated across all history positions) and applies another PersonalizedAttentivePooling layer. Here, the user embedding is again projected through a dense layer to form a query that determines which clicked articles are most relevant for representing the user's current interests. This creates a personalized user representation that weights different historical clicks based on the specific user.

Both the news-level and user-level personalized attention mechanisms share the same user embedding layer, creating a unified learned representation of user preferences that influences the entire encoding pipeline. Click probability is computed via dot product between candidate news and user representations, with softmax during training and sigmoid during single-item inference.

Usage

Use NPAModel when building a news recommendation system where personalization at the feature encoding level is important. NPA is particularly effective in scenarios where users have diverse reading preferences and the same article may appeal to different users for different reasons. It works with the standard MINDIterator since it only requires title-level features (plus user indices), and is best suited when user ID information is available and users have sufficient browsing history.

Code Reference

Source Location

Signature

class NPAModel(BaseModel):
    def __init__(self, hparams, iterator_creator, seed=None)
    def _get_input_label_from_iter(self, batch_data)
    def _build_graph(self)
    def _build_userencoder(self, titleencoder, user_embedding_layer)
    def _build_newsencoder(self, embedding_layer, user_embedding_layer)
    def _build_npa(self)

Import

from recommenders.models.newsrec.models.npa import NPAModel

I/O Contract

Inputs

Name Type Required Description
hparams object Yes Global hyper-parameters including word_emb_dim, user_emb_dim, title_size, his_size, filter_num, window_size, cnn_activation, dropout, attention_hidden_dim, npratio, and wordEmb_file.
iterator_creator object Yes Data iterator creator class (e.g., MINDIterator) for constructing train/test data loaders.
seed int No Random seed for reproducibility of weight initialization.
batch_data dict Yes (at runtime) Dictionary containing "user_index_batch" (int32), "clicked_title_batch" (int64), "candidate_title_batch" (int64), and "labels" (float32).

Outputs

Name Type Description
model keras.Model Training model that takes [user_indexes, his_input_title, pred_input_title] and outputs softmax probabilities over candidates.
scorer keras.Model Inference model that takes [user_indexes, his_input_title, pred_input_title_one] and outputs a sigmoid score for a single candidate.

Usage Examples

Basic Usage

from recommenders.models.newsrec.models.npa import NPAModel
from recommenders.models.newsrec.io.mind_iterator import MINDIterator
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,
    user_emb_dim=50,
    npratio=4,
    his_size=50,
    batch_size=32,
)

# Create the NPA model
model = NPAModel(hparams, MINDIterator, 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.37, ...}

Related Pages

Page Connections

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