OpenNRE: An Open and Extensible Toolkit for Neural Relation Extraction

OpenNRE is an open-source and extensible toolkit that provides a unified framework to implement neural models for relation extraction (RE). Specifically, by implementing typical RE methods, OpenNRE not only allows developers to train custom models to extract structured relational facts from the plain text but also supports quick model validation for researchers. Besides, OpenNRE provides various functional RE modules based on both TensorFlow and PyTorch to maintain sufficient modularity and extensibility, making it becomes easy to incorporate new models into the framework. Besides the toolkit, we also release an online system to meet real-time extraction without any training and deploying. Meanwhile, the online system can extract facts in various scenarios as well as aligning the extracted facts to Wikidata, which may benefit various downstream knowledge-driven applications (e.g., information retrieval and question answering). More details of the toolkit and online system can be obtained from http://github.com/thunlp/OpenNRE.


Introduction
Relation extraction (RE) aims to predict relational facts from the plain text, e.g., extracting (Newton, the Member of, the Royal Society) from the sentence "Newton served as the president of the Royal Society".Because RE models can extract structured information for various downstream applications, many efforts have been devoted to researching RE.As the rapid development of deep learning in the recent years, neural relation extraction (NRE) models show the strong ability to extracting relations and achieve great performance, which makes more and more researchers and industry developers pay attention to this field.
Although the current NRE models are effective and have been applied for various scenarios, including supervised learning paradigm (Zeng et al., 2014a;Nguyen and Grishman, 2015;Zhang et al., 2015;Zhou et al., 2016), distantly supervised learning paradigm (Zeng et al., 2015;Lin et al., 2016;Han et al., 2018b), few-shot learning paradigm (Han et al., 2018c;Gao et al., 2019;Ye and Ling, 2019;Soares et al., 2019;Zhang et al., 2019), there still lack an effective and stable toolkit to support the implementation, deployment and evaluation of models.In fact, for other tasks related to RE, there have been already some effective and long-term maintained toolkits, such as Spacy 1 for named entity recognition (NER), TagMe (Ferragina and Scaiella, 2010) for entity linking (EL), OpenKE (Han et al., 2018a) for knowledge embedding, and Stanford OpenIE (Angeli et al., 2015) for open information extraction.Hence, it becomes necessary and significant to systematically develop an efficient and effective toolkit for RE.
To this end, we develop an open and extensible toolkit for designing and implementing RE models, especially for NRE models, which is named "OpenNRE".The toolkit prioritizes operational efficiency based on TensorFlow and Py-Torch, which support quick model training and validation.Meanwhile, the toolkit maintains sufficient system encapsulation and model extensibility, which can meet some individual requirements of incorporating new models.To keep the ease of use, we implement many typical RE models and provide a unified framework for their data processing, model training, and experimental evaluation.For those developers aiming at training custom The examples of all application scenarios in OpenNRE.models, they can quickly start up their RE system based on OpenNRE, without knowing too many technical details and writing tedious glue code.An online system is also available to extract structured relational facts from the text with friendly interactive interfaces and fast reaction speed.We will provide long-term maintenance to fix bugs and meet new requests for OpenNRE, and we think both researchers and industry developers can benefit from our toolkit.

Application Scenarios
OpenNRE is designed for various scenarios for RE, including sentence-level RE, bag-level RE, document-level RE, and few-shot RE.For completing a full pipeline of extracting structured information, we also enable OpenNRE to have the capacity of entity-oriented applications to a certain extent, e.g., NER and EL.The examples of these application scenarios are all shown in Figure 1.

Entity-Oriented Applications
For extracting structured information from plain text, it requires to extract entities from text and then predict relations between entities.In normal RE scenarios, all entity mentions have been already annotated and RE models are just required to classify relations for all annotated entity pairs.Although the entity-oriented applications are not the focus of our toolkit, we still implement specific modules for NER (Lample et al., 2016) and EL (Han et al., 2011).The NER modules can detect words or phrases (also named entity mentions) representing real-world objects.In Open-NRE, we provide two approaches for NER, one is based on spaCy, the other is based on fine-tuning BERT (Devlin et al., 2019).The EL modules can align those entity mentions to the entities in Wikidata (Vrandečić and Krötzsch, 2014) based on TagMe (Ferragina and Scaiella, 2010).

Sentence-Level Relation Extraction
The conventional methods often handle RE in the supervised learning paradigm and extract the relation between two entities mentioned within one sentence.As shown in Figure 1, each sentence is first manually annotated with two entity mentions.Then models are required to predict the relation between those annotated entity mentions.As there are many efforts to adopt models for this setting (Zeng et al., 2014a;Zhang et al., 2015;Zhou et al., 2016), OpenNRE is specially designed for the sentence-level RE scenario.

Bag-Level Relation Extraction
The supervised RE methods suffer from several problems, especially their requirements of adequate annotated data for training.As manually labeling large amounts of data is expensive and time-consuming, Mintz et al. (2009) introduce distant supervision to automatically label large amounts of data for RE by aligning knowledge graphs and text.Although distant supervision brings sufficient auto-labeled data, it also leads to the wrong labeling problem.Considering an entity pair may occur several times in different sentences, and there is a significant probability that some of these sentences can express the relation between the entity pair.Hence Riedel et al. (2010) and Hoffmann et al. (2011) introduce to aggregate the sentences mentioning the same entity pair into a entity-pair bag.As shown in Figure 1, synthesizing the features of different sentences in a bag can provide more reliable information and result in more accurate predictions.The Bag-level setting is widely applied by various distantly supervised RE methods (Zeng et al., 2015;Lin et al., 2016;Han et al., 2018b), and thus it is also integrated into OpenNRE.

Document-Level Relation Extraction
Yao et al. ( 2019) have pointed out that multiple entities in documents often exhibit complex intersentence relations rather than intra-sentence relations.Besides, as shown in Figure 1, a large number of relational facts are expressed in multiple sentences, e.g., Langdon is the sibling of Jean Clemens.Hence, it is hard to extract these intersentence relations with both the sentence-level and bag-level settings.Although the document-level RE setting is not widely explored by the current work, we argue that this scenario remains an open problem for future research, and still integrate document-level RE into OpenNRE.

Few-Shot Relation Extraction
Though we can train a usable and stable RE system based on the above-mentioned scenarios, which can well predict those relations appearing frequently in data, some long-tail relations with few instances in data are still neglected.Recently, some methods have been proposed to provide a different view of this problem by formalizing RE as a few-shot learning problem (Han et al., 2018c;Gao et al., 2019;Ye and Ling, 2019;Soares et al., 2019;Zhang et al., 2019).As shown in Figure 1, each relation only have a handful of instances in the supporting set in a few-shot RE scenario, and models are required to be capable of accurately capturing relation patterns of these small amounts of training instances.Considering few-shot RE is important for handling long-tail relations, Open-NRE also provides a custom platform for further research in this direction.

Toolkit Design and Implementation
Our goal of designing OpenNRE is achieving the balance among system encapsulation, operational efficiency, model extensibility, and ease of use.
For system encapsulation, we build a unified underlying platform to encapsulate various data processing and training strategies, so that developers can maximize the reuse of code to avoid unnecessary redundant model implementations.For operational efficiency, OpenNRE is based on Tensor-Flow and PyTorch, which enables developers to train models on GPUs.For model extensibility, we systematically implement various neural modules and some special algorithms (e.g., adversarial training (Wu et al., 2017) and reinforcement learning (Feng et al., 2018)).Hence, it is easy to implement new RE models based on OpenNRE.We also implement some typical RE models so as to conveniently train custom models for specific application scenarios.More specifically, OpenNRE attains the above four design objects through implementing the following five components.

Tokenization
The tokenization component is responsible for tokenizing input text into several input tokens.In OpenNRE, we implement both word-level tokenization and subword-level tokenization.These two operations satisfy most tokenization demands and help developers get rid of spending too much time writing glue code for data processing.For building a new tokenizer, extending the BasicTokenizer class and implementing specific tokenization operations is convenient.

Module
The module component consists of various functional neural modules for model implementation, such as the essential network layers, some pooling operations, and activation functions.In order to adapt these modules for RE scenarios, we also implement some special RE neural modules (e.g., piece-wise pooling operation (Zeng et al., 2015)).Using these atomic modules to construct and deploy RE systems has a high degree of freedom.

Encoder
The encoder is applied to encode text into its corresponding embeddings to provide semantic features.In OpenNRE, we implement the BaseEncoder class based on the tokenization and module components, which can provide basic functions of text tokenization and embedding lookup.By extending the BaseEncoder class and implementing specific neural encoding architecture, we can implement various specific encoders.In OpenNRE, we have implemented the common convolutional and recurrent neural encoders, as well as the pre-trained encoder BERT.

Model
Some developers may not require to implement and verify their own RE models, and their main demand is to easily train and deploy custom models.To this end, we also replicate several typical RE models (Zeng et al., 2015;Zhang et al., 2015).Some special algorithms for enhancing RE models are also included in the toolkit, such as attention mechanism (Lin et al., 2016), adversarial training (Wu et al., 2017), and reinforcement learning (Feng et al., 2018).On the one hand, the model component enables us to train custom models without having to understand all technical details.On the other hand, the implemented models in this model component are all tutorial examples to show how to build models with OpenNRE.

Framework
The framework module is mainly responsible for integrating other four components and supporting various functions (including data processing, model training, model optimizing, and model evaluating).In OpenNRE, for all application scenarios mentioned in Section 2, we have implemented their corresponding framework.For other future potential application scenarios, we have also reserved interfaces for their implementation.

Experiment and Evaluation
In this section, we evaluate our toolkit on several benchmark datasets in different RE scenarios.The evaluation results show that our implementation of some state-of-the-art models with OpenNRE can achieve comparable or even better performance, as compared to the original papers.

Sentence-Level Relation Extraction
We experiment on two different encoders for sentence-level relation extraction: CNN (Zeng et al., 2014b) and BERT (Devlin et al., 2019).For CNN, we follow the setting of Nguyen and Grishman (2015), including using word and position embeddings.For BERT, we follow the setting of Soares et al. (2019)."BERT" in our paper refers to using entity markers in input and taking [CLS] as output."BERT-Entity" refers to using entity markers in input and taking entity start as output like Soares et al. (2019).
We carry out experiments on two datasets of sentence-level RE: SemEval 2010 Task-8 (Hendrickx et al., 2009) and Wiki80.SemEval 2010 Task-8 contains 19 relations and 10,717 instances, 17.4% of which are with no relation.Wiki80 is derived from FewRel (Han et al., 2018c), a large scale few-shot dataset.It contains 80 relations and 56,000 instances from Wikipedia and Wikidata (Vrandečić and Krötzsch, 2014).Since Wiki80 is not an official benchmark, we directly report the results on the validation set.From Table 1 we can see that BERT-based models perform bet-   ter than the CNN model and achieve promising results on both datasets.Our implementation of BERT-Entity with OpenNRE achieves comparable results to the original work in Table 2.

Bag-Level Relation Extraction
We implement models with instance-level attention (Lin et al., 2016), adversarial training (Wu et al., 2017) and reinforcement learning (Feng et al., 2018) for bag-level relation extraction.Note that the latter two are based on the instance-level attention mechanism.We use the same CNN encoder as Section 4.1 and denote the three models as "CNN-ATT", "CNN-ADV" and "CNN-RL".
We evaluate those three models on NYT10 (Riedel et al., 2010), a distantly supervised dataset based on New York Times corpus and FreeBase (Bollacker et al., 2008).Table 4 shows that our version of bag-level RE models achieves comparable or even better results than the original papers.

Few-Shot Relation Extraction
We experiment on Prototypical Networks (Snell et al., 2017) for few-shot RE.For the encoder selection, we take CNN and BERT as described in Section 4.1.We also implement a model named "BERT-PAIR", which takes one supporting sentence and one query sentence as input, and directly outputs the probability that they share the same relation with the BERT sequence classification model.The experiments are carried out on FewRel described in Section 4.1.From Table 3 we can see that for both few-shot settings, our version achieves better results than the original results from Han et al. (2018c), proving that our imple-

Online System
Besides the toolkit, we also release an online system.As shown in Figure 3, we train a model in the sentence-level RE scenario and deploy the model for online access.The online system can be directly applied for extracting structured facts from plain text.Meanwhile, all extracted entity mentions and relations can be aligned to Wikidata.

Conclusion
We propose OpenNRE, an open and extensible toolkit for relation extraction.OpenNRE achieves the balance among system encapsulation, operational efficiency, model extensibility, and ease of use.Based on OpenNRE, either training custom models or quick model validation becomes easy.Some experimental results also demonstrate that the models implemented by OpenNRE are efficient and effective, which can achieve comparable or even better performance as compared to the original papers.Furthermore, an online system is also available for meeting real-time extraction without training and deploying.In the future, we will provide long-term maintenance to fix bugs and meet new requests.

Figure 2 :
Figure 2: The architecture and example code of Open-NRE.The structure shows the contents of each part of OpenNRE and how they are related.Based on Open-NRE, one can use only a few lines of code to define, train and evaluate RE models of different scenarios.

Figure 3 :
Figure 3: An example of the online system.

Miller Hemingway was an American journalist Ernest Hemingway was raised in Oak Park, Illinois [Ernest Hemingway] [Oak Park, Illinois]
1 https://spacy.io

Table 1 :
Accuracies of various models on Wiki80 and SemEval 2010 Task-8 under the single sentence setting.

Table 3 :
Accuracies of various models on FewRel under the different few-shot settings."(*)" indicates the original results taken from Han et al. (2018c).

Table 4 :
AUC and F1 scores of various models on NYT10 under the bag-level RE setting."(*)" indicates the original results taken from Lin et al. (2016) and Feng et al. (2018).