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 DKN Item2Item Iterator

From Leeroopedia


Knowledge Sources
Domains Recommendation Systems, Deep Learning, Knowledge-Aware Recommendation
Last Updated 2026-02-10 00:00 GMT

Overview

Implements a specialized data iterator for the DKN item-to-item recommendation variant, providing simplified data loading that omits user behavior history and focuses solely on news article features.

Description

The DKNItem2itemTextIterator class extends DKNTextIterator and simplifies the data pipeline for item-to-item recommendations. Unlike the standard DKN iterator that requires user click history placeholders, this iterator only creates candidate_news_index_batch and candidate_news_entity_index_batch TensorFlow placeholders. The batch size is computed as hparams.batch_size * (neg_num + 2) to accommodate a source item, one positive target, and N negative targets per group.

The _loading_nessary_files method loads a single news feature file, parsing each line into a news ID with its corresponding word indices and entity indices, storing them in self.news_word_index and self.news_entity_index dictionaries.

The load_data_from_file method reads news article IDs from an input file, looks up their word and entity indices, and yields mini-batches as tuples of (feed_dict, newsid_list, data_size). When the final batch is smaller than the required batch size, it pads the batch by cycling through the existing data.

Usage

Use this iterator when training or evaluating the DKN item-to-item model where recommendations are based on article-to-article similarity rather than user-to-article relevance. It is designed for the KDD2020 tutorial workflow.

Code Reference

Source Location

Signature

class DKNItem2itemTextIterator(DKNTextIterator):
    def __init__(self, hparams, graph)

    def _loading_nessary_files(self)

    def load_data_from_file(self, infile)

Import

from recommenders.models.deeprec.io.dkn_item2item_iterator import DKNItem2itemTextIterator

I/O Contract

Inputs

Name Type Required Description
hparams object Yes Global hyper-parameters object containing neg_num, batch_size, doc_size, and news_feature_file
graph tf.Graph Yes The running TensorFlow graph in which to create placeholders
infile (load_data_from_file) str Yes File path where each line is a news article ID

Outputs

Name Type Description
yield (load_data_from_file) tuple(dict, list, int) A tuple of (feed_dict mapping graph elements to numpy arrays, list of news article IDs, batch data size)

Usage Examples

Basic Usage

from recommenders.models.deeprec.io.dkn_item2item_iterator import DKNItem2itemTextIterator

# Initialize the iterator with hyperparameters and a TensorFlow graph
iterator = DKNItem2itemTextIterator(hparams, graph)

# Iterate through batches from an input file
for feed_dict, newsid_list, data_size in iterator.load_data_from_file("news_ids.txt"):
    # feed_dict can be passed directly to sess.run()
    predictions = sess.run(model.pred, feed_dict=feed_dict)

Related Pages

Page Connections

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