Joint Neural Relation Extraction with Text and KGs
Switch branches/tags
Nothing to show
Clone or download
Failed to load latest commit information.
jointD fixes flags' type in Oct 27, 2018
jointE fixes flags' type in Oct 27, 2018
original/baselines no message Mar 13, 2018
LICENSE initial codes and datasets Nov 21, 2017 Update Sep 3, 2018 no message Mar 13, 2018


Codes and datasets for our paper "Neural Knowledge Acquisition via Mutual Attention between Knowledge Graph and Text"

Some Introduction

This implementation is a fast and stable version.

We have made some simplifications for the original model so that to train a joint model just needs around 15min.

We also encapsulate more neural architectures into our framework to encode sentences.

The code and datasets mainly for the task relation extraction.


We provide the datasets used for the task relation extraction.

New York Times Corpus: The data used in relation extraction from text is published by "Modeling relations and their mentions without labeled text". The data should be obtained from [LDC] first.

Datasets are required in the folder data/ in the following format, containing at least 4 files:

  • kg/train.txt: the knowledge graph for training, format (e1, e2, rel).

  • text/relation2id.txt: the relation needed to be predicted for RE, format (rel, id).

  • text/train.txt: the text for training, format (e1, e2, name1, name2, rel, sentence).

  • text/vec.txt: the initial word embeddings.

  • [Download]

Run the experiments

To run the experiments, unpack the datasets first:

unzip -d origin_data/
mkdir data/

Run the corresponding python scripts to train models:

cd jointE

Change the corresponding python code to set hyperparameters:'nbatch_kg',100,'entity numbers used each training time')'margin',1.0,'entity numbers used each training time')'learning_rate_kg',0.001,'learning rate for kg')'ent_total',lib.getEntityTotal(),'total of entities')'rel_total',lib.getRelationTotal(),'total of relations')'tri_total',lib.getTripleTotal(),'total of triples')'katt_flag', 1, '1 for katt, 0 for att')'model', 'cnn', 'neural models to encode sentences')'max_length',config['fixlen'],'maximum of number of words in one sentence')'pos_num', config['maxlen'] * 2 + 1,'number of position embedding vectors')'num_classes', config['textual_rel_total'],'maximum of relations')'hidden_size',230,'hidden feature size')'pos_size',5,'position embedding size')'max_epoch',20,'maximum of training epochs')'batch_size',160,'entity numbers used each training time')'learning_rate',0.5,'learning rate for nn')'weight_decay',0.00001,'weight_decay')'keep_prob',0.5,'dropout rate')'model_dir','./model/','path to store model')'summary_dir','./summary','path to store summary_dir')

Run the corresponding python scripts to test models:

cd jointE

Note that the hyperparameters in the and the must be the same.

Run the corresponding python script to get PR-curve results:

cd jointE


   title={Neural Knowledge Acquisition via Mutual Attention between Knowledge Graph and Text},
   author={Han, Xu and Liu, Zhiyuan and Sun, Maosong},
   booktitle={Proceedings of AAAI},