examples/07_tutorials/KDD2020-tutorial/step3_run_dkn.ipynb
<i>Copyright (c) Recommenders contributors.</i>
<i>Licensed under the MIT License.</i>
DKN [1] is a deep learning model which incorporates information from knowledge graph for better news recommendation. Specifically, DKN uses TransX [2] method for knowledge graph representaion learning, then applies a CNN framework, named KCNN, to combine entity embedding with word embedding and generate a final embedding vector for a news article. CTR prediction is made via an attention-based neural scorer.
[label] [userid] [CandidateNews]%[impressionid]
e.g., 1 train_U1 N1%0
[Userid] [newsid1,newsid2...]
e.g., train_U1 N1,N2
[Newsid] [w1,w2,w3...wk] [e1,e2,e3...ek]
from recommenders.models.deeprec.deeprec_utils import *
from recommenders.models.deeprec.models.dkn import *
from recommenders.models.deeprec.io.dkn_iterator import *
import time
import tensorflow as tf
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
Usually we will debug and search hyper-parameters on a small dataset. You can switch between the small dataset and full dataset by changing the value of tag.
tag = 'small' # small or full
data_path = 'data_folder/my/DKN-training-folder'
yaml_file = './dkn.yaml' # os.path.join(data_path, r'../../../../../../dkn.yaml')
train_file = os.path.join(data_path, r'train_{0}.txt'.format(tag))
valid_file = os.path.join(data_path, r'valid_{0}.txt'.format(tag))
test_file = os.path.join(data_path, r'test_{0}.txt'.format(tag))
user_history_file = os.path.join(data_path, r'user_history_{0}.txt'.format(tag))
news_feature_file = os.path.join(data_path, r'../paper_feature.txt')
wordEmb_file = os.path.join(data_path, r'word_embedding.npy')
entityEmb_file = os.path.join(data_path, r'entity_embedding.npy')
contextEmb_file = os.path.join(data_path, r'context_embedding.npy')
infer_embedding_file = os.path.join(data_path, r'infer_embedding.txt')
epoch=5
hparams = prepare_hparams(yaml_file,
news_feature_file = news_feature_file,
user_history_file = user_history_file,
wordEmb_file=wordEmb_file,
entityEmb_file=entityEmb_file,
contextEmb_file=contextEmb_file,
epochs=epoch,
is_clip_norm=True,
max_grad_norm=0.5,
history_size=20,
MODEL_DIR=os.path.join(data_path, 'save_models'),
learning_rate=0.001,
embed_l2=0.0,
layer_l2=0.0,
use_entity=True,
use_context=True
)
print(hparams.values())
input_creator = DKNTextIterator
model = DKN(hparams, input_creator)
t01 = time.time()
print(model.run_eval(valid_file))
t02 = time.time()
print((t02-t01)/60)
model.fit(train_file, valid_file)
Now we can test again the performance on valid set:
t01 = time.time()
print(model.run_eval(test_file))
t02 = time.time()
print((t02-t01)/60)
After training, you can get document embedding through this document embedding inference API. The input file format is same with document feature file. The output file fomrat is: [Newsid] [embedding]
model.run_get_embedding(news_feature_file, infer_embedding_file)
we compre with DKN performance between using knowledge entities or without using knowledge entities (DKN(-)):
| Models | Group-AUC | MRR | NDCG@2 | NDCG@4 |
|---|---|---|---|---|
| DKN | 0.9557 | 0.8993 | 0.8951 | 0.9123 |
| DKN(-) | 0.9506 | 0.8817 | 0.8758 | 0.8982 |
| LightGCN | 0.8608 | 0.5605 | 0.4975 | 0.5792 |
[1] Wang, Hongwei, et al. "DKN: Deep Knowledge-Aware Network for News Recommendation." Proceedings of the 2018 World Wide Web Conference on World Wide Web. International World Wide Web Conferences Steering Committee, 2018.