Implementation:Recommenders team Recommenders DKN Model
| Knowledge Sources | |
|---|---|
| Domains | Recommender Systems, Deep Learning, Knowledge Graphs, News Recommendation |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Implements the Deep Knowledge-Aware Network (DKN) model for news recommendation, incorporating knowledge graph entity and context embeddings alongside word embeddings through a Knowledge-aware CNN (KCNN) architecture.
Description
The DKN class extends BaseModel and implements the DKN architecture from Wang et al. (WWW 2018). The model uniquely leverages knowledge graph information to enhance news understanding and user modeling for news recommendation.
During initialization, three types of embeddings are created: (1) word embeddings loaded from a pre-trained Word2Vec file and set as trainable; (2) entity embeddings loaded from a knowledge graph embedding file and optionally transformed through a linear layer with tanh activation to match the word embedding dimension; (3) context embeddings similarly loaded and transformed from the knowledge graph. If entity or context features are disabled via hyperparameters, zero-initialized trainable variables are used instead.
The core architecture is built through _build_dkn which calls _build_pair_attention to generate both user and candidate news embeddings. The KCNN module (_kims_cnn) is an extension of Kim's CNN that concatenates word, entity, and optionally context embeddings for each news article, then applies multiple CNN filters of different sizes (e.g., [1, 2, 3]) with max-pooling to produce fixed-length document representations.
User embeddings are computed via a candidate-aware attention mechanism over the user's clicked news article embeddings. The attention network takes the concatenation of each clicked article embedding and the candidate article embedding, passes it through a hidden layer, and produces a softmax-normalized attention weight. The final user representation is the weighted sum of clicked article embeddings.
The prediction is made by concatenating the user and candidate news embeddings and passing them through a fully-connected DNN with batch normalization support. The model also provides infer_embedding and run_get_embedding methods for extracting and exporting news article embeddings.
The class overrides _l1_loss and _l2_loss from the base class to add regularization over the word, entity, and context embedding variables.
Usage
Instantiate DKN with hyperparameters that include paths to pre-trained word embeddings, entity embeddings, and optionally context embeddings. Use with a DKN-specific data iterator that provides candidate news indices, clicked news indices, and their corresponding entity indices.
Code Reference
Source Location
- Repository: Recommenders
- File: recommenders/models/deeprec/models/dkn.py
- Lines: 1-486
Signature
class DKN(BaseModel):
"""DKN model (Deep Knowledge-Aware Network)"""
def __init__(self, hparams, iterator_creator):
def _init_embedding(self, file_path):
def _l2_loss(self):
def _l1_loss(self):
def _build_graph(self):
def _build_dkn(self):
def _build_pair_attention(
self, candidate_word_batch, candidate_entity_batch,
click_word_batch, click_entity_batch, hparams,
):
def _kims_cnn(self, word, entity, hparams):
def infer_embedding(self, sess, feed_dict):
def run_get_embedding(self, infile_name, outfile_name):
Import
from recommenders.models.deeprec.models.dkn import DKN
I/O Contract
Inputs (__init__)
| Name | Type | Required | Description |
|---|---|---|---|
| hparams | HParams | Yes | Global hyperparameters object containing model configuration |
| iterator_creator | callable | Yes | DKN data loader class that provides news word indices, entity indices, and click history batches |
Key Hyperparameters
| Name | Type | Description |
|---|---|---|
| wordEmb_file | str | Path to the pre-trained word embedding file (NumPy .npy format) |
| entityEmb_file | str | Path to the entity embedding file from knowledge graph (NumPy .npy format) |
| contextEmb_file | str | Path to the context embedding file from knowledge graph (NumPy .npy format) |
| use_entity | bool | Whether to use knowledge graph entity embeddings |
| use_context | bool | Whether to use knowledge graph context embeddings |
| entity_dim | int | Dimension of entity embeddings before transformation |
| dim | int | Target dimension for word and transformed entity/context embeddings |
| doc_size | int | Number of words per news document |
| history_size | int | Number of clicked news articles per user |
| filter_sizes | list | CNN filter sizes (e.g., [1, 2, 3])
|
| num_filters | int | Number of CNN filters per filter size |
| attention_layer_sizes | int | Hidden size of the attention network |
| attention_activation | str | Activation function for the attention hidden layer |
| entity_size | int | Total number of entities in the knowledge graph |
Inputs (run_get_embedding)
| Name | Type | Required | Description |
|---|---|---|---|
| infile_name | str | Yes | Input file with format [Newsid] [w1,w2,w3...] [e1,e2,e3...]
|
| outfile_name | str | Yes | Output file path with format [Newsid] [embedding]
|
Outputs
| Name | Type | Description |
|---|---|---|
| logit | tf.Tensor | Prediction logit from the DNN output layer (inherited from BaseModel for loss computation) |
| news_field_embed_final_batch | tf.Tensor | Candidate news article embeddings, accessible via infer_embedding
|
| self | DKN | The model instance (returned by run_get_embedding)
|
Usage Examples
Basic Usage
from recommenders.models.deeprec.models.dkn import DKN
from recommenders.models.deeprec.io.dkn_iterator import DKNTextIterator
from recommenders.models.deeprec.deeprec_utils import prepare_hparams
# Prepare hyperparameters
hparams = prepare_hparams(
"dkn.yaml",
wordEmb_file="word_embeddings.npy",
entityEmb_file="entity_embeddings.npy",
contextEmb_file="context_embeddings.npy",
use_entity=True,
use_context=True,
doc_size=10,
history_size=50,
dim=100,
entity_dim=100,
filter_sizes=[1, 2, 3],
num_filters=50,
learning_rate=0.0001,
epochs=10,
)
# Initialize and train the DKN model
model = DKN(hparams, DKNTextIterator)
model.fit(
train_file="train.txt",
valid_file="valid.txt",
)
# Evaluate
eval_res = model.run_eval("test.txt")
print(eval_res)
Extracting News Embeddings
# After training, extract news embeddings for downstream use
model.run_get_embedding(
infile_name="news_features.txt",
outfile_name="news_embeddings.txt",
)
# Output format: NewsID embedding_val1,embedding_val2,...
Related Pages
Depends On
Requires Environment
- Environment:Recommenders_team_Recommenders_Python_Core_Dependencies
- Environment:Recommenders_team_Recommenders_GPU_CUDA_Environment