Implementation:Recommenders team Recommenders DKN Item2Item Model
| Knowledge Sources | |
|---|---|
| Domains | Recommendation Systems, Deep Learning, Knowledge-Aware Recommendation |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Adapts the DKN (Deep Knowledge-aware Network) model for item-to-item recommendations, computing similarity between news articles using knowledge-aware document embeddings rather than user-to-article relevance.
Description
The DKNItem2Item class extends the base DKN model and overrides several key methods to support item-to-item recommendation.
The _build_dkn method generates L2-normalized document embeddings via _build_doc_embedding, reshapes them into groups of (source item, positive target, N negative targets), and computes dot-product similarity scores between the source item and all target items using tf.math.multiply followed by a sum reduction. The result is a tensor of shape (batch_size, neg_num + 1) stored as self.pred_logits.
The _build_doc_embedding method adds a dense tanh transformation layer on top of the KCNN (Knowledge-aware Convolutional Neural Network) output using a learned weight matrix W_doc_trans, producing dense document embeddings.
The _get_pred method applies softmax over the similarity scores. The _compute_data_loss method uses negative log-likelihood of the first (positive) item's softmax score as the training loss.
The eval method generates synthetic labels where the first item in each group is marked as positive (label=1), since ground truth is implied by item ordering. The run_eval method evaluates the model using pairwise metrics via cal_metric.
Usage
Use this model for finding similar news articles based on content and knowledge graph entity similarity. It is designed for item-to-item news recommendation scenarios as demonstrated in the KDD2020 tutorial.
Code Reference
Source Location
- Repository: Recommenders
- File: recommenders/models/deeprec/models/dkn_item2item.py
- Lines: 1-129
Signature
class DKNItem2Item(DKN):
def _compute_data_loss(self)
def _build_dkn(self)
def _get_pred(self, logit, task)
def _build_doc_embedding(self, candidate_word_batch, candidate_entity_batch)
def eval(self, sess, feed_dict)
def run_eval(self, filename)
Import
from recommenders.models.deeprec.models.dkn_item2item import DKNItem2Item
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| sess (eval) | tf.Session | Yes | The TensorFlow model session object |
| feed_dict (eval) | dict | Yes | Feed values dictionary mapping graph elements to numpy arrays |
| filename (run_eval) | str | Yes | Path to evaluation data file containing news article IDs |
| candidate_word_batch (_build_doc_embedding) | tf.Tensor | Yes | Batch of word indices for candidate news articles |
| candidate_entity_batch (_build_doc_embedding) | tf.Tensor | Yes | Batch of entity indices for candidate news articles |
Outputs
| Name | Type | Description |
|---|---|---|
| return (_build_dkn) | tf.Tensor | Prediction logits of item-to-item similarity scores in shape (batch_size, neg_num + 1) |
| return (eval) | tuple(numpy.ndarray, numpy.ndarray) | A tuple of (predictions, labels) arrays for evaluation |
| return (run_eval) | dict | Dictionary containing pairwise evaluation metrics |
Usage Examples
Basic Usage
from recommenders.models.deeprec.models.dkn_item2item import DKNItem2Item
from recommenders.models.deeprec.io.dkn_item2item_iterator import DKNItem2itemTextIterator
from recommenders.models.deeprec.deeprec_utils import prepare_hparams
# Prepare hyperparameters and create the model
hparams = prepare_hparams(yaml_file, news_feature_file=news_feature_file)
model = DKNItem2Item(hparams, DKNItem2itemTextIterator)
# Train the model
model.fit(train_file)
# Evaluate using pairwise metrics
eval_results = model.run_eval(test_file)
print(eval_results) # e.g., {'auc': 0.85, 'ndcg': 0.72}