An Open-Source Package for Neural Relation Extraction (NRE)

Overview

OpenNRE

CircleCI

We have a DEMO website (http://opennre.thunlp.ai/). Try it out!

OpenNRE is an open-source and extensible toolkit that provides a unified framework to implement relation extraction models. This package is designed for the following groups:

  • New to relation extraction: We have hand-by-hand tutorials and detailed documents that can not only enable you to use relation extraction tools, but also help you better understand the research progress in this field.
  • Developers: Our easy-to-use interface and high-performance implementation can acclerate your deployment in the real-world applications. Besides, we provide several pretrained models which can be put into production without any training.
  • Researchers: With our modular design, various task settings and metric tools, you can easily carry out experiments on your own models with only minor modification. We have also provided several most-used benchmarks for different settings of relation extraction.
  • Anyone who need to submit an NLP homework to impress their professors: With state-of-the-art models, our package can definitely help you stand out among your classmates!

This package is mainly contributed by Tianyu Gao, Xu Han, Shulian Cao, Lumin Tang, Yankai Lin, Zhiyuan Liu

What is Relation Extraction

Relation extraction is a natural language processing (NLP) task aiming at extracting relations (e.g., founder of) between entities (e.g., Bill Gates and Microsoft). For example, from the sentence Bill Gates founded Microsoft, we can extract the relation triple (Bill Gates, founder of, Microsoft).

Relation extraction is a crucial technique in automatic knowledge graph construction. By using relation extraction, we can accumulatively extract new relation facts and expand the knowledge graph, which, as a way for machines to understand the human world, has many downstream applications like question answering, recommender system and search engine.

How to Cite

A good research work is always accompanied by a thorough and faithful reference. If you use or extend our work, please cite the following paper:

@inproceedings{han-etal-2019-opennre,
    title = "{O}pen{NRE}: An Open and Extensible Toolkit for Neural Relation Extraction",
    author = "Han, Xu and Gao, Tianyu and Yao, Yuan and Ye, Deming and Liu, Zhiyuan and Sun, Maosong",
    booktitle = "Proceedings of EMNLP-IJCNLP: System Demonstrations",
    year = "2019",
    url = "https://www.aclweb.org/anthology/D19-3029",
    doi = "10.18653/v1/D19-3029",
    pages = "169--174"
}

It's our honor to help you better explore relation extraction with our OpenNRE toolkit!

Papers and Document

If you want to learn more about neural relation extraction, visit another project of ours (NREPapers).

You can refer to our document for more details about this project.

Install

Install as A Python Package

We are now working on deploy OpenNRE as a Python package. Coming soon!

Using Git Repository

Clone the repository from our github page (don't forget to star us!)

git clone https://github.com/thunlp/OpenNRE.git

If it is too slow, you can try

git clone https://github.com/thunlp/OpenNRE.git --depth 1

Then install all the requirements:

pip install -r requirements.txt

Note: Please choose appropriate PyTorch version based on your machine (related to your CUDA version). For details, refer to https://pytorch.org/.

Then install the package with

python setup.py install 

If you also want to modify the code, run this:

python setup.py develop

Note that we have excluded all data and pretrain files for fast deployment. You can manually download them by running scripts in the benchmark and pretrain folders. For example, if you want to download FewRel dataset, you can run

bash benchmark/download_fewrel.sh

Easy Start

Make sure you have installed OpenNRE as instructed above. Then import our package and load pre-trained models.

>>> import opennre
>>> model = opennre.get_model('wiki80_cnn_softmax')

Note that it may take a few minutes to download checkpoint and data for the first time. Then use infer to do sentence-level relation extraction

>>> model.infer({'text': 'He was the son of Máel Dúin mac Máele Fithrich, and grandson of the high king Áed Uaridnach (died 612).', 'h': {'pos': (18, 46)}, 't': {'pos': (78, 91)}})
('father', 0.5108704566955566)

You will get the relation result and its confidence score.

For now, we have the following available models:

  • wiki80_cnn_softmax: trained on wiki80 dataset with a CNN encoder.
  • wiki80_bert_softmax: trained on wiki80 dataset with a BERT encoder.
  • wiki80_bertentity_softmax: trained on wiki80 dataset with a BERT encoder (using entity representation concatenation).
  • tacred_bert_softmax: trained on TACRED dataset with a BERT encoder.
  • tacred_bertentity_softmax: trained on TACRED dataset with a BERT encoder (using entity representation concatenation).

Training

You can train your own models on your own data with OpenNRE. In example folder we give example training codes for supervised RE models and bag-level RE models. You can either use our provided datasets or your own datasets.

Google Group

If you want to receive our update news or take part in discussions, please join our Google Group

Comments
  • Using another dataset

    Using another dataset

    Hello

    I would like to use OpenNRE to train a French model.

    1. I already have the data which I obtained by distant supervision (DBpedia + Wikipedia) and I'm trying to figure out into what structure I need to get it to be able to use it on OpenNRE. I looked at the examples in the benchmark folder: all the datasets (except semeval), in each sentence, have an id for each entity. How should I replicate this in my dataset? Generating random unique numbers to represent each entity in my dataset?

    2. I know that in the NYT dataset, if an entity has more than one word, its word embedding is obtained by the concatenation of the multiple words. If I want to use French word embeddings do I need to train them to expect this? (did you train your BERT embedding too?)

    3. I also noticed that the mentions of the entities in sentences were replaced by their Freebase form (e.g., in a sentence that mentioned Obama, we have instead Barack Obama). Should I do the same for my dataset or leaving the entities as they are mentioned in the text ok (I see there can be possibly be problems when trying to group them in bags)

    4. Do the word embeddings I use need to be in any special format?

    5. In the examples folder I see examples for training 3 different kinds of models. Do I need to train a specific kind of model because I use distant supervison?

    opened by CatarinaPC 19
  • Test set accuracy does not match with published result

    Test set accuracy does not match with published result

    As shown in the home page, test set accuracy should be around 0.8, while it is around 0.50 on my machine for all test tasks. I'm using tensorflow 1.0.0 and fixed some API migration issues before I could successfully train and test the model, and I was evaluating P@N for iter 14000. Below I pasted the training info and testing info. Please let me know where I could get it wrong. Thank you so much:)

    have saved model to ./model/ATT_GRU_model-13500 2017-04-02T03:08:14.022144: step 13550, softmax_loss 8.99307, acc 0.94 2017-04-02T03:08:24.764260: step 13600, softmax_loss 11.1679, acc 0.92 2017-04-02T03:08:35.354639: step 13650, softmax_loss 4.43873, acc 0.98 2017-04-02T03:08:45.710377: step 13700, softmax_loss 12.744, acc 0.94 2017-04-02T03:08:55.166245: step 13750, softmax_loss 2.57561, acc 0.98 2017-04-02T03:09:04.721789: step 13800, softmax_loss 2.85167, acc 0.98 2017-04-02T03:09:14.571793: step 13850, softmax_loss 2.9708, acc 0.98 2017-04-02T03:09:23.866147: step 13900, softmax_loss 4.50743, acc 0.98 2017-04-02T03:09:34.195987: step 13950, softmax_loss 5.79872, acc 0.96 2017-04-02T03:09:44.283621: step 14000, softmax_loss 10.5228, acc 0.92 saving model have saved model to ./model/ATT_GRU_model-14000 out of range 2017-04-02T03:10:17.838734: step 14050, softmax_loss 2.30686, acc 0.98 2017-04-02T03:10:28.560130: step 14100, softmax_loss 5.12479, acc 0.98 2017-04-02T03:10:38.362117: step 14150, softmax_loss 8.63566, acc 0.94 2017-04-02T03:10:48.828123: stepI tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 56277731 get requests, put_count=56277740 evicted_count=17000 eviction_rate=0.000302073 and unsatisfied allocation rate=0.000307226 14200, softmax_loss 5.62947, acc 0.98 2017-04-02T03:10:59.361231: step 14250, softmax_loss 4.52537, acc 0.98 2017-04-02T03:11:08.783026: step 14300, softmax_loss 32.7668, acc 0.84 2017-04-02T03:11:19.310379: step 14350, softmax_loss 10.8961, acc 0.94 2017-04-02T03:11:29.238714: step 14400, softmax_loss 7.69886, acc 0.94 2017-04-02T03:11:38.763790: step 14450, softmax_loss 14.5586, acc 0.92 2017-04-02T03:11:48.848076: step 14500, softmax_loss 14.7915, acc 0.96

    ============================================================

    Evaluating P@N for iter 14000 Evaluating P@N for one I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 7082 get requests, put_count=1102 evicted_count=1000 eviction_rate=0.907441 and unsatisfied allocation rate=0.999718 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:259] Raising pool_size_limit_ from 100 to 110 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 0 get requests, put_count=6011 evicted_count=6000 eviction_rate=0.99817 and unsatisfied allocation rate=0 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 0 get requests, put_count=4013 evicted_count=4000 eviction_rate=0.996761 and unsatisfied allocation rate=0 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 0 get requests, put_count=3016 evicted_count=3000 eviction_rate=0.994695 and unsatisfied allocation rate=0 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 0 get requests, put_count=1019 evicted_count=1000 eviction_rate=0.981354 and unsatisfied allocation rate=0 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 4716 get requests, put_count=5254 evicted_count=5000 eviction_rate=0.951656 and unsatisfied allocation rate=0.950594 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:259] Raising pool_size_limit_ from 233 to 256 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 0 get requests, put_count=3025 evicted_count=3000 eviction_rate=0.991736 and unsatisfied allocation rate=0 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 0 get requests, put_count=2030 evicted_count=2000 eviction_rate=0.985222 and unsatisfied allocation rate=0 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 0 get requests, put_count=1037 evicted_count=1000 eviction_rate=0.96432 and unsatisfied allocation rate=0 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 7082 get requests, put_count=7491 evicted_count=7000 eviction_rate=0.934455 and unsatisfied allocation rate=0.936317 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:259] Raising pool_size_limit_ from 449 to 493 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 0 get requests, put_count=6049 evicted_count=6000 eviction_rate=0.991899 and unsatisfied allocation rate=0 P@100: 0.53 P@200: 0.525 P@300: 0.51 Evaluating P@N for two I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 0 get requests, put_count=5059 evicted_count=5000 eviction_rate=0.988338 and unsatisfied allocation rate=0 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 0 get requests, put_count=5072 evicted_count=5000 eviction_rate=0.985804 and unsatisfied allocation rate=0 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 0 get requests, put_count=5087 evicted_count=5000 eviction_rate=0.982898 and unsatisfied allocation rate=0 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 0 get requests, put_count=5105 evicted_count=5000 eviction_rate=0.979432 and unsatisfied allocation rate=0 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 7082 get requests, put_count=6529 evicted_count=5000 eviction_rate=0.765814 and unsatisfied allocation rate=0.802033 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:259] Raising pool_size_limit_ from 1400 to 1540 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 0 get requests, put_count=1169 evicted_count=1000 eviction_rate=0.855432 and unsatisfied allocation rate=0 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 0 get requests, put_count=1225 evicted_count=1000 eviction_rate=0.816327 and unsatisfied allocation rate=0 P@100: 0.56 P@200: 0.535 P@300: 0.503333333333 Evaluating P@N for all I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 7082 get requests, put_count=7271 evicted_count=4000 eviction_rate=0.550131 and unsatisfied allocation rate=0.576532 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:259] Raising pool_size_limit_ from 2997 to 3296 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 0 get requests, put_count=3398 evicted_count=3000 eviction_rate=0.882872 and unsatisfied allocation rate=0 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 11798 get requests, put_count=12147 evicted_count=4000 eviction_rate=0.329299 and unsatisfied allocation rate=0.350314 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:259] Raising pool_size_limit_ from 5305 to 5835 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:247] PoolAllocator: After 11798 get requests, put_count=11832 evicted_count=1000 eviction_rate=0.0845166 and unsatisfied allocation rate=0.141634 I tensorflow/core/common_runtime/gpu/pool_allocator.cc:259] Raising pool_size_limit_ from 7764 to 8540 P@100: 0.57 P@200: 0.535 P@300: 0.523333333333 2017-04-02T22:13:33.351736 Evaluating all test data and save data for PR curve saving all test result... PR curve area:0.257896324167 2017-04-02T22:14:57.170137 P@N for all test data: P@100: 0.52 P@200: 0.505 P@300: 0.476666666667

    opened by cloudfengchao 18
  • 关于评估方式

    关于评估方式

    你好!想问下基于bag-level的评估方式(计算precision, recall)和基于sentence-level的评估方式是否是等价的?假如我以sentence-level的方式训练模型,分别以bag-level方式和sentence-level方式进行评估,结果(precision, recall, AUC, F1)是否会有差异?如果有差异哪种方式指标会更高些? 谢谢!

    opened by ShellingFord221 15
  • many relations and few entpairs problem

    many relations and few entpairs problem

    I run the test_demo.py file, and get result.json as follow:

    [{"relation": 1, "score": 0.0, "entpair": "MT00015574#MT00001615"}, {"relation": 2, "score": 0.0, "entpair": "MT00015574#MT00001615"}, {"relation": 3, "score": 1.0377574859848236e-23, "entpair": "MT00015574#MT00001615"}, {"relation": 4, "score": 3.1533666815183214e-17, "entpair": "MT00015574#MT00001615"}, {"relation": 5, "score": 1.0, "entpair": "MT00015574#MT00001615"}, {"relation": 6, "score": 0.0, "entpair": "MT00015574#MT00001615"}, {"relation": 7, "score": 0.0, "entpair": "MT00015574#MT00001615"}, {"relation": 8, "score": 0.0, "entpair": "MT00015574#MT00001615"}, {"relation": 9, "score": 3.0276185570896847e-38, "entpair": "MT00015574#MT00001615"}, {"relation": 10, "score": 1.6583352665328436e-21, "entpair": "MT00015574#MT00001615"}, {"relation": 11, "score": 1.1470061012097688e-12, "entpair": "MT00015574#MT00001615"}, {"relation": 12, "score": 0.0, "entpair": "MT00015574#MT00001615"}, {"relation": 13, "score": 0.0, "entpair": "MT00015574#MT00001615"}, {"relation": 14, "score": 6.406954060414727e-37, "entpair": "MT00015574#MT00001615"}, {"relation": 15, "score": 1.0, "entpair": "MT00015574#MT00001615"}, {"relation": 16, "score": 0.0, "entpair": "MT00015574#MT00001615"}, {"relation": 17, "score": 4.0768455202450195e-29, "entpair": "MT00015574#MT00001615"}, {"relation": 18, "score": 1.1802251857395389e-17, "entpair": "MT00015574#MT00001615"}, {"relation": 19, "score": 2.1728631039285967e-32, "entpair": "MT00015574#MT00001615"}, {"relation": 20, "score": 1.0, "entpair": "MT00015574#MT00001615"}, {"relation": 21, "score": 1.6063005092714633e-11, "entpair": "MT00015574#MT00001615"}, {"relation": 22, "score": 0.0, "entpair": "MT00015574#MT00001615"},

    {"relation": 1, "score": 0.0, "entpair": "MT00015574#MT00000621"}, {"relation": 2, "score": 0.0, "entpair": "MT00015574#MT00000621"}, {"relation": 3, "score": 1.5956034921966758e-27, "entpair": "MT00015574#MT00000621"}, {"relation": 4, "score": 3.524426855707885e-34, "entpair": "MT00015574#MT00000621"}, {"relation": 5, "score": 1.0, "entpair": "MT00015574#MT00000621"}, {"relation": 6, "score": 3.198225596035746e-18, "entpair": "MT00015574#MT00000621"}, {"relation": 7, "score": 0.0, "entpair": "MT00015574#MT00000621"}, {"relation": 8, "score": 0.0, "entpair": "MT00015574#MT00000621"}, {"relation": 9, "score": 1.8198349964571598e-31, "entpair": "MT00015574#MT00000621"}, {"relation": 10, "score": 4.6186643822339425e-30, "entpair": "MT00015574#MT00000621"}, {"relation": 11, "score": 0.0, "entpair": "MT00015574#MT00000621"}, {"relation": 12, "score": 0.0, "entpair": "MT00015574#MT00000621"}, {"relation": 13, "score": 0.0, "entpair": "MT00015574#MT00000621"}, {"relation": 14, "score": 2.5636822667148816e-37, "entpair": "MT00015574#MT00000621"}, {"relation": 15, "score": 0.9877334833145142, "entpair": "MT00015574#MT00000621"}, {"relation": 16, "score": 0.0, "entpair": "MT00015574#MT00000621"}, {"relation": 17, "score": 0.0, "entpair": "MT00015574#MT00000621"}, {"relation": 18, "score": 9.379044019787019e-26, "entpair": "MT00015574#MT00000621"}, {"relation": 19, "score": 0.7510679960250854, "entpair": "MT00015574#MT00000621"}, {"relation": 20, "score": 0.7352021932601929, "entpair": "MT00015574#MT00000621"}, {"relation": 21, "score": 3.3799745229219635e-14, "entpair": "MT00015574#MT00000621"}, {"relation": 22, "score": 0.0, "entpair": "MT00015574#MT00000621"}]

    I have a dataset with 22 relations for training, and the entpairs of them are only MT00015574#MT00000621 and MT00015574#MT00001615. And then I put the test dataset with a thousand sentences, whose entities are already recognized, but no relations, and I want to find them with our method. However, I only get two results and I don't know which sentence do they represent.

    I debug the project and I see the parameter 'iter_output' in framework.py. Only the first two index has value and the others are zero.

    I think it relates to my entpairs, but how can I see the predicted relations for each sentence. Thanks a lot

    question 
    opened by MelvinZang 14
  • in ubuntu Illegal instruction (core dumped)

    in ubuntu Illegal instruction (core dumped)

    i'm getting an error like this for cnn based model.

    import opennre model = opennre.get_model('wiki80_cnn_softmax') 2020-12-01 21:21:37,001 - root - INFO - Initializing word embedding with word2vec. model.infer({'text': 'He was the son of Máel Dúin mac Máele Fithrich, and grandson of the high king Áed Uaridnach (died 612).', 'h': {'pos': (18, 46)}, 't': {'pos': (78, 91)}}) Illegal instruction (core dumped)

    for bert based model im getting like this.

    import opennre ^[[A model = opennre.get_model('wiki80_bert_softmax') 2020-12-01 21:22:38,692 - root - INFO - Loading BERT pre-trained checkpoint. 2020-12-01 21:22:38,693 - transformers.configuration_utils - INFO - loading configuration file /home/aby/.opennre/pretrain/bert-base-uncased/config.json 2020-12-01 21:22:38,693 - transformers.configuration_utils - INFO - Model config BertConfig { "attention_probs_dropout_prob": 0.1, "gradient_checkpointing": false, "hidden_act": "gelu", "hidden_dropout_prob": 0.1, "hidden_size": 768, "initializer_range": 0.02, "intermediate_size": 3072, "layer_norm_eps": 1e-12, "max_position_embeddings": 512, "model_type": "bert", "num_attention_heads": 12, "num_hidden_layers": 12, "pad_token_id": 0, "type_vocab_size": 2, "vocab_size": 30522 }

    2020-12-01 21:22:38,694 - transformers.modeling_utils - INFO - loading weights file /home/aby/.opennre/pretrain/bert-base-uncased/pytorch_model.bin 2020-12-01 21:22:42,759 - transformers.modeling_utils - INFO - All model checkpoint weights were used when initializing BertModel.

    2020-12-01 21:22:42,759 - transformers.modeling_utils - INFO - All the weights of BertModel were initialized from the model checkpoint at /home/aby/.opennre/pretrain/bert-base-uncased. If your task is similar to the task the model of the ckeckpoint was trained on, you can already use BertModel for predictions without further training. 2020-12-01 21:22:42,760 - transformers.tokenization_utils_base - INFO - Model name '/home/aby/.opennre/pretrain/bert-base-uncased' not found in model shortcut name list (bert-base-uncased, bert-large-uncased, bert-base-cased, bert-large-cased, bert-base-multilingual-uncased, bert-base-multilingual-cased, bert-base-chinese, bert-base-german-cased, bert-large-uncased-whole-word-masking, bert-large-cased-whole-word-masking, bert-large-uncased-whole-word-masking-finetuned-squad, bert-large-cased-whole-word-masking-finetuned-squad, bert-base-cased-finetuned-mrpc, bert-base-german-dbmdz-cased, bert-base-german-dbmdz-uncased, TurkuNLP/bert-base-finnish-cased-v1, TurkuNLP/bert-base-finnish-uncased-v1, wietsedv/bert-base-dutch-cased). Assuming '/home/aby/.opennre/pretrain/bert-base-uncased' is a path, a model identifier, or url to a directory containing tokenizer files. 2020-12-01 21:22:42,776 - transformers.tokenization_utils_base - INFO - Didn't find file /home/aby/.opennre/pretrain/bert-base-uncased/added_tokens.json. We won't load it. 2020-12-01 21:22:42,776 - transformers.tokenization_utils_base - INFO - Didn't find file /home/aby/.opennre/pretrain/bert-base-uncased/special_tokens_map.json. We won't load it. 2020-12-01 21:22:42,777 - transformers.tokenization_utils_base - INFO - Didn't find file /home/aby/.opennre/pretrain/bert-base-uncased/tokenizer_config.json. We won't load it. 2020-12-01 21:22:42,777 - transformers.tokenization_utils_base - INFO - Didn't find file /home/aby/.opennre/pretrain/bert-base-uncased/tokenizer.json. We won't load it. 2020-12-01 21:22:42,777 - transformers.tokenization_utils_base - INFO - loading file /home/aby/.opennre/pretrain/bert-base-uncased/vocab.txt 2020-12-01 21:22:42,777 - transformers.tokenization_utils_base - INFO - loading file None 2020-12-01 21:22:42,777 - transformers.tokenization_utils_base - INFO - loading file None 2020-12-01 21:22:42,778 - transformers.tokenization_utils_base - INFO - loading file None 2020-12-01 21:22:42,778 - transformers.tokenization_utils_base - INFO - loading file None Traceback (most recent call last): File "", line 1, in File "/home/aby/Desktop/OpenNRE/venv/lib/python3.8/site-packages/opennre-0.1-py3.8.egg/opennre/pretrain.py", line 140, in get_model File "/home/aby/Desktop/OpenNRE/venv/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1044, in load_state_dict raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format( RuntimeError: Error(s) in loading state_dict for SoftmaxNN: Unexpected key(s) in state_dict: "sentence_encoder.bert.embeddings.position_ids".

    im stuck at this for 2 days. can anyone help me resolve it. ASAP.

    Thanks and regards

    opened by abyaug 13
  • I can't reproduce the results of your paper through this framework

    I can't reproduce the results of your paper through this framework

    I run example/train_nyt10_pcnn_att.py, but the result are not up to those reported in this paper "Neural Relation Extraction with Selective Attention over Instances". Are there any problems in your reproduce code?

    opened by superwars 12
  • Question about accuracy --> 0

    Question about accuracy --> 0

    Hi,

    a question: I applied OpenNRE both with our own dataset (created via DBpedia) and KBP37. When calling train_demo.py, eg. with the KBP37 datasets, during Epoch 1, loss is around 1.7-2.0, and accuracy at 0.18. Then at the end of Epoch 1, loss goes to 'inf', and starting from Epoch 2 accuracy is 0.0, and it stays there.

    The same thing happens with my other dataset (based on DBpedia).

    I am new to DL, is there something I do wrong, or is the dataset too small, ...?

    Thank you very much for the feedback, Gerhard

    bash $ python2.7 train_demo.py 
    Dataset Name kbp37 
    
    Pre-processed files exist. Loading them...
    Finish loading
    Total relation fact: 12628
    Pre-processed files exist. Loading them...
    Finish loading
    Total relation fact: 3295
    Start training...
    2018-10-23 10:02:47.737580: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
    Calculating weights_table...
    train_demo.py:99: RuntimeWarning: divide by zero encountered in divide
      _weights_table = 1 / (_weights_table ** 0.05)
    Finish calculating
    ###### Epoch 0 ######
    epoch 0 step 78 time 0.79 | loss: inf, not NA accuracy: 0.178651, accuracy: 0.17848177804
    Average iteration time: 0.860235
    Testing...
    Calculating weights_table...
    Finish calculating
    [TEST] step 20 | not NA accuracy: 0.000000, accuracy: 0.036012
    [TEST] auc: 0.0468855111775
    Finish testing
    Best model, storing...
    Finish storing
    ###### Epoch 1 ######
    epoch 1 step 78 time 0.88 | loss: nan, not NA accuracy: 0.000000, accuracy: 0.000949
    Average iteration time: 0.822871
    Testing...
    Calculating weights_table...
    Finish calculating
    [TEST] step 20 | not NA accuracy: 0.000000, accuracy: 0.036012
    [TEST] auc: 0.0468855111775
    Finish testing
    ###### Epoch 2 ######
    epoch 2 step 78 time 0.87 | loss: nan, not NA accuracy: 0.000000, accuracy: 0.000949
    Average iteration time: 0.844874
    Testing...
    Calculating weights_table...
    Finish calculating
    [TEST] step 20 | not NA accuracy: 0.000000, accuracy: 0.036012
    [TEST] auc: 0.0468855111775
    Finish testing
    ###### Epoch 3 ######
    epoch 3 step 78 time 0.86 | loss: nan, not NA accuracy: 0.000000, accuracy: 0.000949
    Average iteration time: 0.885802
    Testing...
    Calculating weights_table...
    Finish calculating
    [TEST] step 20 | not NA accuracy: 0.000000, accuracy: 0.036012
    [TEST] auc: 0.0468855111775
    Finish testing
    ###### Epoch 4 ######
    epoch 4 step 78 time 0.83 | loss: nan, not NA accuracy: 0.000000, accuracy: 0.000949
    Average iteration time: 0.877218
    Testing...
    Calculating weights_table...
    Finish calculating
    [TEST] step 20 | not NA accuracy: 0.000000, accuracy: 0.036012
    [TEST] auc: 0.0468855111775
    Finish testing
    
    opened by gwohlgen 12
  • Precision-recall curve and P@N

    Precision-recall curve and P@N

    Looking at the code on the eval() function, data_loader.py script in the BagREDataset class:

    def eval(self, pred_result):
            """
            Args:
                pred_result: a list with dict {'entpair': (head_id, tail_id), 'relation': rel, 'score': score}.
                    Note that relation of NA should be excluded.
            Return:
                {'prec': narray[...], 'rec': narray[...], 'mean_prec': xx, 'f1': xx, 'auc': xx}
                    prec (precision) and rec (recall) are in micro style.
                    prec (precision) and rec (recall) are sorted in the decreasing order of the score.
                    f1 is the max f1 score of those precison-recall points
            """
            sorted_pred_result = sorted(pred_result, key=lambda x: x['score'], reverse=True)
            prec = []
            rec = []
            correct = 0
            total = len(self.facts)
            for i, item in enumerate(sorted_pred_result):
                if (item['entpair'][0], item['entpair'][1], item['relation']) in self.facts:
                    correct += 1
                prec.append(float(correct) / float(i + 1))
                rec.append(float(correct) / float(total))
            auc = sklearn.metrics.auc(x=rec, y=prec)
            np_prec = np.array(prec)
            np_rec = np.array(rec) 
            f1 = (2 * np_prec * np_rec / (np_prec + np_rec + 1e-20)).max()
            mean_prec = np_prec.mean()
            return {'micro_p': np_prec, 'micro_r': np_rec, 'micro_p_mean': mean_prec, 'micro_f1': f1, 'auc': auc}
    
    

    If I want to show the precision-recall curve do I just need plot np_prec on the y axis and np_rec on the x axis (without the need of another function) ?

    The same goes for computing P@N (Precision@N), for different N's, e.g., N=100, N=200, N=300. Do I just need to look at the index N in the np_prec array?

    opened by CatarinaPC 11
  • Question about example

    Question about example

    when I run train_nyt10_pcnn_att.py,the error shows: Traceback (most recent call last): File "train_nyt10_pcnn_att.py", line 21, in opennre.download_nyt10(root_path=root_path) AttributeError: module 'opennre' has no attribute 'download_nyt10'

    opened by qkl123 11
  • Not able to reproduce the results

    Not able to reproduce the results

    I changed the codes to be compatible with TF1.3 and run the training and test. The performance I got is lower than reported. Any clue?

    Evaluating P@N for iter 11000 Evaluating P@N for one P@100: 0.73 P@200: 0.645 P@300: 0.6 Evaluating P@N for two P@100: 0.77 P@200: 0.69 P@300: 0.636666666667 Evaluating P@N for all P@100: 0.77 P@200: 0.695 P@300: 0.66 2017-10-24T19:14:49.392740 Evaluating all test data and save data for PR curve saving all test result... PR curve area:0.347798385666 2017-10-24T19:17:38.815224 P@N for all test data: P@100: 0.78 P@200: 0.72 P@300: 0.69

    Thanks, Lisheng

    opened by fufrank5 11
  • RuntimeError: DefaultCPUAllocator: can't allocate memory

    RuntimeError: DefaultCPUAllocator: can't allocate memory

    I was running the example code in example/train_bag_pcnn_att.py

    I replaced the paths used to load the variables word2id and word2vec with files that I created from a fasttext .vec file. I got an error: 'Object arrays cannot be loaded when allow_pickle=False' on: word2vec = np.load(word2vec_path) I changed it to: word2vec = np.load(word2vec_path, allow_pickle=True)and that error got fixed.

    From reading online its an error related to keras and numpy.


    I ran the code again and got another error (which is the topic of this issue):

    Traceback (most recent call last):
      File "/home/catarinapc/distant-re/src/models/train_model.py", line 14, in <module>
        run()
      File "/home/catarinapc/distant-re/src/models/train_model.py", line 12, in run
        word2vec_path='../../models/word_embeddings/fasttext/wiki.fr_mat.npy')
      File "/home/catarinapc/distant-re/src/models/bag_pcnn_att_model.py", line 37, in train
        dropout=0.5
      File "/home/catarinapc/distant-re/venv/lib/python3.6/site-packages/opennre-0.1-py3.6.egg/opennre/encoder/pcnn_encoder.py", line 39, in __init__
      File "/home/catarinapc/distant-re/venv/lib/python3.6/site-packages/opennre-0.1-py3.6.egg/opennre/encoder/base_encoder.py", line 56, in __init__
      File "/home/catarinapc/distant-re/venv/lib/python3.6/site-packages/torch/nn/modules/sparse.py", line 109, in __init__
        self.weight = Parameter(torch.Tensor(num_embeddings, embedding_dim))
    RuntimeError: [enforce fail at CPUAllocator.cpp:64] . DefaultCPUAllocator: can't allocate memory: you tried to allocate 5312573229600 bytes. Error code 12 (Cannot allocate memory)
    

    On:

            # Define the sentence encoder
            sentence_encoder = opennre.encoder.PCNNEncoder(
                token2id=word2id,
                max_length=128,
                word_size=50,
                position_size=5,
                hidden_size=230,
                blank_padding=True,
                kernel_size=3,
                padding_size=1,
                word2vec=word2vec,
                dropout=0.5
            )
    

    How should I proceed?

    opened by CatarinaPC 9
  • Question about BagOne

    Question about BagOne

    I was checking the BagOne class, and on this line:

    https://github.com/thunlp/OpenNRE/blob/0acd945d9fc94761a10e721af385c7cdb9e55997/opennre/model/bag_one.py#L46

    the method sentence_encoder.tokenizer was being used in this way:

    token, pos1, pos2, mask = self.sentence_encoder.tokenize(item)

    However, aren't the position of pos1 and masks flipped? And, what if the masks entry is not being returned? E.g. see here:

    Different order of the returned values: https://github.com/thunlp/OpenNRE/blob/master/opennre/encoder/bert_encoder.py#L215

    No masks returned: https://github.com/thunlp/OpenNRE/blob/0acd945d9fc94761a10e721af385c7cdb9e55997/opennre/encoder/base_encoder.py#L154

    question 
    opened by pminervini 0
  • NYT10m Experiments -- Manual Evaluation Matters

    NYT10m Experiments -- Manual Evaluation Matters

    Hi,

    Thank you for the latest contribution "Manual Evaluation Matters: Reviewing Test Protocols of Distantly Supervised Relation Extraction", having manual test significantly improves our understanding of the DRE models. I have few questions re the paper's experiments:

    Q1: Is it possible to provide the pre-trained checkpoints for BERT+sent/bag+AVG models?

    Q2: Regarding evaluation, it is mentioned in paper:

    Bag-level manual evaluation: We take our
    human-labeled test data for bag-level evaluation.
    Since annotated data are at the sentence-level, we
    construct bag-level annotations in the following
    way: For each bag, if one sentence in the bag has
    a human-labeled relation, this bag is labeled with
    this relation; if no sentence in the bag is annotated
    with any relation, this bag is labeled as N/A.
    

    Can you elaborate this further, is this same as in current eval part of BagRELoader code? Unfortunately, I cannot find 'anno_relation_list' in the manually created test set, does this require additional pre-processing?

    Q3: At evaluation (valid, test) time, the bag_size parameter should be set to 0 (so we consider all sentences in the Bag as also reported in paper -- but this is not handled in current BagRE framework) and entpair_as_bag to True?

    Q4: Can you provide the scores for the NYT10m val set for the models reported in Table 4 of the paper? Do you also plan to provide P@k metrics and pr_curves for the models reported in Table 4?

    Q5: Is BERT+sent level training performed with MultiLabelSentenceRE or simple SentenceRE?

    Thank you in advance!

    enhancement 
    opened by suamin 2
  • Setup improvements

    Setup improvements

    Included requirements in setup.py so at some point this package could be uploaded to PyPI and to simplify instructions. Next step would be to loosen up constrains from requirements.txt

    opened by rooterkyberian 0
  • Cannot replicate wiki_distant results

    Cannot replicate wiki_distant results

    Hello,

    I couldn't replicate the AUC performance for Wikidistant (22.2%) found in this paper: https://www.aclweb.org/anthology/2020.aacl-main.75.pdf, using this current train_bag_pcnn_att.py script. I am getting approximately 16-17% instead. Could you share the hyper-parameters you used?

    Thank you!

    question 
    opened by fenchri 0
Owner
THUNLP
Natural Language Processing Lab at Tsinghua University
THUNLP
Utilizing RBERT model for KLUE Relation Extraction task

RBERT for Relation Extraction task for KLUE Project Description Relation Extraction task is one of the task of Korean Language Understanding Evaluatio

snoop2head 14 Nov 15, 2022
Spert NLP Relation Extraction API deployed with torchserve for inference

SpERT torchserve Spert_torchserve is the Relation Extraction model (SpERT)Span-based Entity and Relation Transformer API deployed with pytorch/serve.

Zichu Chen 1 Nov 24, 2021
Code to reproduce the results of the paper 'Towards Realistic Few-Shot Relation Extraction' (EMNLP 2021)

Realistic Few-Shot Relation Extraction This repository contains code to reproduce the results in the paper "Towards Realistic Few-Shot Relation Extrac

Bloomberg 8 Nov 9, 2022
open-information-extraction-system, build open-knowledge-graph(SPO, subject-predicate-object) by pyltp(version==3.4.0)

中文开放信息抽取系统, open-information-extraction-system, build open-knowledge-graph(SPO, subject-predicate-object) by pyltp(version==3.4.0)

null 7 Nov 2, 2022
:hot_pepper: R²SQL: "Dynamic Hybrid Relation Network for Cross-Domain Context-Dependent Semantic Parsing." (AAAI 2021)

R²SQL The PyTorch implementation of paper Dynamic Hybrid Relation Network for Cross-Domain Context-Dependent Semantic Parsing. (AAAI 2021) Requirement

huybery 60 Dec 31, 2022
Open Source Neural Machine Translation in PyTorch

OpenNMT-py: Open-Source Neural Machine Translation OpenNMT-py is the PyTorch version of the OpenNMT project, an open-source (MIT) neural machine trans

OpenNMT 5.8k Jan 4, 2023
Open Source Neural Machine Translation in PyTorch

OpenNMT-py: Open-Source Neural Machine Translation OpenNMT-py is the PyTorch version of the OpenNMT project, an open-source (MIT) neural machine trans

OpenNMT 4.8k Feb 18, 2021
Simple, Pythonic, text processing--Sentiment analysis, part-of-speech tagging, noun phrase extraction, translation, and more.

TextBlob: Simplified Text Processing Homepage: https://textblob.readthedocs.io/ TextBlob is a Python (2 and 3) library for processing textual data. It

Steven Loria 8.4k Dec 26, 2022
Python implementation of TextRank for phrase extraction and summarization of text documents

PyTextRank PyTextRank is a Python implementation of TextRank as a spaCy pipeline extension, used to: extract the top-ranked phrases from text document

derwen.ai 1.9k Jan 6, 2023
Simple, Pythonic, text processing--Sentiment analysis, part-of-speech tagging, noun phrase extraction, translation, and more.

TextBlob: Simplified Text Processing Homepage: https://textblob.readthedocs.io/ TextBlob is a Python (2 and 3) library for processing textual data. It

Steven Loria 7.5k Feb 17, 2021
Python implementation of TextRank for phrase extraction and summarization of text documents

PyTextRank PyTextRank is a Python implementation of TextRank as a spaCy pipeline extension, used to: extract the top-ranked phrases from text document

derwen.ai 1.4k Feb 17, 2021
SpikeX - SpaCy Pipes for Knowledge Extraction

SpikeX is a collection of pipes ready to be plugged in a spaCy pipeline. It aims to help in building knowledge extraction tools with almost-zero effort.

Erre Quadro Srl 384 Dec 12, 2022
Mirco Ravanelli 2.3k Dec 27, 2022
Datasets of Automatic Keyphrase Extraction

This repository contains 20 annotated datasets of Automatic Keyphrase Extraction made available by the research community. Following are the datasets and the original papers that proposed them. If you know more datasets, and want to contribute, please, notify me.

LIAAD - Laboratory of Artificial Intelligence and Decision Support 163 Dec 23, 2022
Code for "Generating Disentangled Arguments with Prompts: a Simple Event Extraction Framework that Works"

GDAP The code of paper "Code for "Generating Disentangled Arguments with Prompts: a Simple Event Extraction Framework that Works"" Event Datasets Prep

null 45 Oct 29, 2022
Blue Brain text mining toolbox for semantic search and structured information extraction

Blue Brain Search Source Code DOI Data & Models DOI Documentation Latest Release Python Versions License Build Status Static Typing Code Style Securit

The Blue Brain Project 29 Dec 1, 2022
Perform sentiment analysis and keyword extraction on Craigslist listings

craiglist-helper synopsis Perform sentiment analysis and keyword extraction on Craigslist listings Background I love Craigslist. I've found most of my

Mark Musil 1 Nov 8, 2021
Contact Extraction with Question Answering.

contactsQA Extraction of contact entities from address blocks and imprints with Extractive Question Answering. Goal Input: Dr. Max Mustermann Hauptstr

Jan 2 Apr 20, 2022
A toolkit for document-level event extraction, containing some SOTA model implementations

Document-level Event Extraction via Heterogeneous Graph-based Interaction Model with a Tracker Source code for ACL-IJCNLP 2021 Long paper: Document-le

null 84 Dec 15, 2022