{"id":13488406,"url":"https://github.com/yuanxiaosc/Multiple-Relations-Extraction-Only-Look-Once","last_synced_at":"2025-03-28T01:35:06.746Z","repository":{"id":54527082,"uuid":"185574675","full_name":"yuanxiaosc/Multiple-Relations-Extraction-Only-Look-Once","owner":"yuanxiaosc","description":"Multiple-Relations-Extraction-Only-Look-Once. Just look at the sentence once and extract the multiple pairs of entities and their corresponding relations. 端到端联合多关系抽取模型，可用于 http://lic2019.ccf.org.cn/kg 信息抽取。","archived":false,"fork":false,"pushed_at":"2019-07-25T01:46:43.000Z","size":244,"stargazers_count":347,"open_issues_count":8,"forks_count":69,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-03-26T04:08:33.689Z","etag":null,"topics":["bert-model","entity-extraction","information-extraction","joint-models","relation-extraction","tensorflow-models"],"latest_commit_sha":null,"homepage":"https://yuanxiaosc.github.io/2019/05/28/信息抽取任务相关论文发展脉络/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yuanxiaosc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-08T09:28:01.000Z","updated_at":"2025-02-05T13:14:34.000Z","dependencies_parsed_at":"2022-08-13T18:40:46.887Z","dependency_job_id":null,"html_url":"https://github.com/yuanxiaosc/Multiple-Relations-Extraction-Only-Look-Once","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuanxiaosc%2FMultiple-Relations-Extraction-Only-Look-Once","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuanxiaosc%2FMultiple-Relations-Extraction-Only-Look-Once/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuanxiaosc%2FMultiple-Relations-Extraction-Only-Look-Once/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuanxiaosc%2FMultiple-Relations-Extraction-Only-Look-Once/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yuanxiaosc","download_url":"https://codeload.github.com/yuanxiaosc/Multiple-Relations-Extraction-Only-Look-Once/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952768,"owners_count":20699536,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bert-model","entity-extraction","information-extraction","joint-models","relation-extraction","tensorflow-models"],"created_at":"2024-07-31T18:01:15.196Z","updated_at":"2025-03-28T01:35:03.751Z","avatar_url":"https://github.com/yuanxiaosc.png","language":"Python","readme":"# Multiple-Relations-Extraction-Only-Look-Once\nMultiple-Relations-Extraction-Only-Look-Once. Just look at the sentence once and extract the multiple pairs of entities and their corresponding relations.\n只用看一次，抽取所有的实体及其**对应的**所有关系。\n\n**input**\n```\"text\": \"《逐风行》是百度文学旗下纵横中文网签约作家清水秋风创作的一部东方玄幻小说，小说已于2014-04-28正式发布\"```\n\n**output** \n```\n\"spo_list\": [{\"predicate\": \"连载网站\", \"object_type\": \"网站\", \"subject_type\": \"网络小说\", \"object\": \"纵横中文网\", \"subject\": \"逐风行\"}, {\"predicate\": \"作者\", \"object_type\": \"人物\", \"subject_type\": \"图书作品\", \"object\": \"清水秋风\", \"subject\": \"逐风行\"}]\n```\n\n## Main principle\n\n![](https://yuanxiaosc.github.io/2019/05/28/%E4%BF%A1%E6%81%AF%E6%8A%BD%E5%8F%96%E4%BB%BB%E5%8A%A1%E7%9B%B8%E5%85%B3%E8%AE%BA%E6%96%87%E5%8F%91%E5%B1%95%E8%84%89%E7%BB%9C/model-2018Bekoulis.png)\n\n![](https://yuanxiaosc.github.io/2019/05/28/%E4%BF%A1%E6%81%AF%E6%8A%BD%E5%8F%96%E4%BB%BB%E5%8A%A1%E7%9B%B8%E5%85%B3%E8%AE%BA%E6%96%87%E5%8F%91%E5%B1%95%E8%84%89%E7%BB%9C/model-2018Verga.png)\n\n\nThe entity extraction task is converted into a sequence annotation task, and the multi-relationship extraction task is converted into a [multi-head selection task](https://arxiv.org/abs/1804.07847). The token will be sent to the model, and the model will predict the output label, predicate value, and predicate location. Using the bin/read_standard_format_data.py file, the original game data format can be converted into the data format required for the multi-head selection model, as follows:\n\n把实体抽取任务转换成序列标注任务，把多关系抽取任务转换成[多头选择任务](https://arxiv.org/abs/1804.07847)。token会被送入模型，模型会预测输出label、predicate value 和 predicate location。使用bin/read_standard_format_data.py文件可以把原始比赛数据格式转换成多头选择模型所需数据格式，如下所示：\n\n```\n+-------+-------+------------+----------------------+--------------------+\n| index | token |   label    |   predicate value    | predicate location |\n+-------+-------+------------+----------------------+--------------------+\n|   0   |   《  |     O      |        ['N']         |        [0]         |\n|   1   |   逐  | B-图书作品  | ['连载网站', '作者']  |      [12, 21]      |\n|   2   |   风  | I-图书作品  |        ['N']         |        [2]         |\n|   3   |   行  | I-图书作品  |        ['N']         |        [3]         |\n|   4   |   》  |     O      |        ['N']         |        [4]         |\n|   5   |   是  |     O      |        ['N']         |        [5]         |\n|   6   |   百  |     O      |        ['N']         |        [6]         |\n|   7   |   度  |     O      |        ['N']         |        [7]         |\n|   8   |   文  |     O      |        ['N']         |        [8]         |\n|   9   |   学  |     O      |        ['N']         |        [9]         |\n|   10  |   旗  |     O      |        ['N']         |        [10]        |\n|   11  |   下  |     O      |        ['N']         |        [11]        |\n|   12  |   纵  |   B-网站   |        ['N']         |        [12]        |\n|   13  |   横  |   I-网站   |        ['N']         |        [13]        |\n|   14  |   中  |   I-网站   |        ['N']         |        [14]        |\n|   15  |   文  |   I-网站   |        ['N']         |        [15]        |\n|   16  |   网  |   I-网站   |        ['N']         |        [16]        |\n|   17  |   签  |     O      |        ['N']         |        [17]        |\n|   18  |   约  |     O      |        ['N']         |        [18]        |\n|   19  |   作  |     O      |        ['N']         |        [19]        |\n|   20  |   家  |     O      |        ['N']         |        [20]        |\n|   21  |   清  |   B-人物   |        ['N']         |        [21]        |\n|   22  |   水  |   I-人物   |        ['N']         |        [22]        |\n|   23  |   秋  |   I-人物   |        ['N']         |        [23]        |\n|   24  |   风  |   I-人物   |        ['N']         |        [24]        |\n|   25  |   创  |     O      |        ['N']         |        [25]        |\n...\n+-------+-------+------------+----------------------+--------------------+\n```\n\n\u003e [See my blog for more details](https://yuanxiaosc.github.io/2019/05/09/Multiple-Relations-Extraction-Only-Look-Once/)\n\n\n## Document description\n\n|name|description|\n|-|-|\n|bert|The feature extractor of the model, where BERT is used as the feature extractor for the model. It can be replaced with other feature extractors, such as BiLSTM and CNN.|\n|bin/data_manager.py|Prepare formatted data for the mode.|\n|bin/integrated_model_output.py|Organize the output of model prediction into standard data format.|\n|bin/read_standard_format_data.py|View formatted data.|\n|bin/test_head_select_scores.py|One method for solving the relationship extraction problem: the multi-head selection method.|\n|experimental_loss_function|run_multiple_relations_extraction_XXX.py, File under experiment.|\n|raw_data||\n|produce_submit_json_file.py|Generate entity relationship triples and write them to JSON files.|\n|run_multiple_relations_extraction.py|The most basic model for model training and prediction.|\n\n### Need Your Help!\n\n+ Well-designed loss function!\n+ https://github.com/bekou/multihead_joint_entity_relation_extraction/issues/11\n+ Improve the speed of produce_submit_json_file.py to generate entity relationship files, and change to parallel processing!\n\n**Problem code location**\n\u003e run_multiple_relations_extraction.py 531~549 lines!\n\nYou can try different experiments (experimental_loss_function/run_multiple_relations_extraction_XXX.py) and share your results.\n\n## Use example\n\n### [2019语言与智能技术竞赛](http://lic2019.ccf.org.cn/kg)\n\n#### 竞赛任务\n给定schema约束集合及句子sent，其中schema定义了关系P以及其对应的主体S和客体O的类别，例如（S_TYPE:人物，P:妻子，O_TYPE:人物）、（S_TYPE:公司，P:创始人，O_TYPE:人物）等。 任务要求参评系统自动地对句子进行分析，输出句子中所有满足schema约束的SPO三元组知识Triples=[(S1, P1, O1), (S2, P2, O2)…]。\n输入/输出:\n(1) 输入:schema约束集合及句子sent\n(2) 输出:句子sent中包含的符合给定schema约束的三元组知识Triples\n\n**例子**\n输入句子： ```\"text\": \"《古世》是连载于云中书城的网络小说，作者是未弱\"```\n\n输出三元组： ```\"spo_list\": [{\"predicate\": \"作者\", \"object_type\": \"人物\", \"subject_type\": \"图书作品\", \"object\": \"未弱\", \"subject\": \"古世\"}, {\"predicate\": \"连载网站\", \"object_type\": \"网站\", \"subject_type\": \"网络小说\", \"object\": \"云中书城\", \"subject\": \"古世\"}]}```\n\n#### 数据简介\n本次竞赛使用的SKE数据集是业界规模最大的基于schema的中文信息抽取数据集，其包含超过43万三元组数据、21万中文句子及50个已定义好的schema，表1中展示了SKE数据集中包含的50个schema及对应的例子。数据集中的句子来自百度百科和百度信息流文本。数据集划分为17万训练集，2万验证集和2万测试集。其中训练集和验证集用于训练，可供自由下载，测试集分为两个，测试集1供参赛者在平台上自主验证，测试集2在比赛结束前一周发布，不能在平台上自主验证，并将作为最终的评测排名。\n\n\n### Getting Started\n\n#### Environment Requirements\n+ python 3.6+\n+ Tensorflow 1.12.0+\n\n#### Step 1: Environmental preparation\n+ Install Tensorflow \n+ Dowload [bert-base, chinese](https://storage.googleapis.com/bert_models/2018_11_03/chinese_L-12_H-768_A-12.zip), unzip file and put it in ```pretrained_model``` floader.\n\n#### Step 2: Download the training data, dev data and schema files\nPlease download the training data, development data and schema files from [the competition website](http://lic2019.ccf.org.cn/kg), then unzip files and put them in ```./raw_data/``` folder.\n```\ncd data\nunzip train_data.json.zip \nunzip dev_data.json.zip\ncd -\n```\n\n此处不再提供2019语言与智能技术竞赛_信息抽取原始数据下载，如有疑问可以联系我的邮箱 wangzichaochaochao@gmail.com\n\nThere is no longer a raw data download, if you have any questions, you can contact my mailbox wangzichaochaochao@gmail.com\n\n#### Step3: Data preprocessing\n```\npython bin/data_manager.py\n```\n\n\u003e It is currently recommended to use the run_multiple_relations_extraction_MSE_loss.py file instead of the run_multiple_relations_extraction.py file for model and forecasting!\n\n#### Step4： Model training\n\nRun_multiple_relations_extraction_mask_loss.py is recommended.\n\n```\npython run_multiple_relations_extraction.py \\\n--task_name=SKE_2019 \\\n--do_train=true \\\n--do_eval=false \\\n--data_dir=bin/standard_format_data \\\n--vocab_file=pretrained_model/chinese_L-12_H-768_A-12/vocab.txt \\\n--bert_config_file=pretrained_model/chinese_L-12_H-768_A-12/bert_config.json \\\n--init_checkpoint=pretrained_model/chinese_L-12_H-768_A-12/bert_model.ckpt \\\n--max_seq_length=128 \\\n--train_batch_size=32 \\\n--learning_rate=2e-5 \\\n--num_train_epochs=3.0 \\\n--output_dir=./output_model/multiple_relations_model/epochs3/\n```\n\n#### Step5: Model prediction\n```\npython run_multiple_relations_extraction.py \\\n  --task_name=SKE_2019 \\\n  --do_predict=true \\\n  --data_dir=bin/standard_format_data \\\n  --vocab_file=pretrained_model/chinese_L-12_H-768_A-12/vocab.txt \\\n  --bert_config_file=pretrained_model/chinese_L-12_H-768_A-12/bert_config.json \\\n  --init_checkpoint=output_model/multiple_relations_model/epochs3/model.ckpt-2000 \\\n  --max_seq_length=128 \\\n  --output_dir=./infer_out/multiple_relations_model/epochs3/ckpt2000\n```\n\n#### Step6: Generating Entities and Relational Files\n```\npython produce_submit_json_file.py\n```\n\n\u003e You can use other strategies to generate the final entity relationship file for better results. I have written the template code for you (see produce_submit_json_file.py).\n\n## Paper realization\nThis code is an unofficial implementation of [Extracting Multiple-Relations in One-Pass with Pre-Trained Transformers](https://arxiv.org/abs/1902.01030) and [Joint entity recognition and relation extraction as a multi-head selection problem](https://arxiv.org/abs/1804.07847).\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuanxiaosc%2FMultiple-Relations-Extraction-Only-Look-Once","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyuanxiaosc%2FMultiple-Relations-Extraction-Only-Look-Once","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuanxiaosc%2FMultiple-Relations-Extraction-Only-Look-Once/lists"}