{"id":13535242,"url":"https://github.com/lvjianxin/Knowledge-extraction","last_synced_at":"2025-04-02T00:33:09.479Z","repository":{"id":40256039,"uuid":"158905936","full_name":"lvjianxin/Knowledge-extraction","owner":"lvjianxin","description":"基于中文的知识抽取，BaseLine：Bi-LSTM+CRF","archived":false,"fork":false,"pushed_at":"2018-12-28T06:47:34.000Z","size":12548,"stargazers_count":44,"open_issues_count":1,"forks_count":12,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-02T23:32:43.464Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/lvjianxin.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":"2018-11-24T05:38:12.000Z","updated_at":"2024-03-19T11:21:06.000Z","dependencies_parsed_at":"2022-09-12T17:51:45.900Z","dependency_job_id":null,"html_url":"https://github.com/lvjianxin/Knowledge-extraction","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/lvjianxin%2FKnowledge-extraction","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lvjianxin%2FKnowledge-extraction/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lvjianxin%2FKnowledge-extraction/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lvjianxin%2FKnowledge-extraction/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lvjianxin","download_url":"https://codeload.github.com/lvjianxin/Knowledge-extraction/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246735358,"owners_count":20825222,"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":[],"created_at":"2024-08-01T08:00:51.887Z","updated_at":"2025-04-02T00:33:07.662Z","avatar_url":"https://github.com/lvjianxin.png","language":"Python","funding_links":[],"categories":["BERT  Knowledge Graph Task :","Tasks"],"sub_categories":["Knowledge Graph"],"readme":"### **基于中文的知识抽取，BaseLine：Bi-LSTM+CRF**\n\n**Overview**\n\n基于字的BiLSTM-CRF，使用Bakeoff-3评测中所采用的的BIO标注集，即B-PER、I-PER代表人名首字、人名非首字，B-LOC、I-LOC代表地名首字、地名非首字，B-ORG、I-ORG代表组织机构名首字、组织机构名非首字，O代表该字不属于命名实体的一部分，也可以采用更复杂的BIOSE标注集。\n\n以句子为单位，将一个含有 n 个字的句子（字的序列）记作\n\t  \n`x=(x1,x2,...,xn)`\n\n其中 xi 表示句子的第 i 个字在字典中的id，进而可以得到每个字的one-hot向量，维数是字典大小。\n\n**模型**\n\n![Bi-LSTM标签预测原理图](https://github.com/lvjianxin/Knowledge-extraction/blob/master/img-folder/webp.webp.jpg)\n\n\n第一层：look-up 层，利用预训练或随机初始化的embedding矩阵将句子中的每个字 xi 由one-hot向量映射为低维稠密的字向量（character embedding）xi∈Rd ，d 是embedding的维度。在输入下一层之前，设置dropout以缓解过拟合。\n\n第二层：双向LSTM层，自动提取句子特征。将一个句子的各个字的char embedding序列 (x1,x2,...,xn) 作为双向LSTM各个时间步的输入，再将正向LSTM输出的隐状态序列 (h1⟶,h2⟶,...,hn⟶) 与反向LSTM的 (h1⟵,h2⟵,...,hn⟵) 在各个位置输出的隐状态进行按位置拼接 ht=[ht⟶;ht⟵]∈Rm，得到完整的隐状态序列。\n\n第三层：是CRF层，进行句子级的序列标注。CRF层的参数是一个 (k+2)×(k+2) 的矩阵 A ，Aij 表示的是从第 i 个标签到第 j个标签的转移得分，进而在为一个位置进行标注的时候可以利用此前已经标注过的标签，之所以要加2是因为要为句子首部添加一个起始状态以及为句子尾部添加一个终止状态。\n\n**CRF作用**\n\n由于BiLSTM的输出为单元的每一个标签分值，我们可以挑选分值最高的一个作为该单元的标签。例如，对于单元w0,“B-Person”有最高分值—— 1.5，因此我们可以挑选“B-Person”作为w0的预测标签。同理，我们可以得到w1——“I-Person”，w2—— “O” ，w3——“B-Organization”，w4——“O”。\n\n虽然我们可以得到句子x中每个单元的正确标签，但是我们不能保证标签每次都是预测正确的。例如，图2.中的例子，标签序列是“I-Organization I-Person” and “B-Organization I-Person”，很显然这是错误的。\n\n![去除CRF层的BiLSTM模型](https://github.com/lvjianxin/Knowledge-extraction/blob/master/img-folder/2.png)\n\n在神经网络的输出层接入CRF层(重点是利用标签转移概率)来做句子级别的标签预测，使得标注过程不再是对各个token独立分类。biLSTM计算出的是每个词的各标签概率，而CRF层引入序列的转移概率，最终计算出loss反馈回网络。\n\n**CRF层能从训练数据中获得约束性的规则**\n\nCRF层可以为最后预测的标签添加一些约束来保证预测的标签是合法的。在训练数据训练过程中，这些约束可以通过CRF层自动学习到。\n\n这些约束可以是：\n\nI：句子中第一个词总是以标签“B-“ 或 “O”开始，而不是“I-”\n\nII：标签“B-label1 I-label2 I-label3 I-…”,label1, label2, label3应该属于同一类实体。例如，“B-Person I-Person” 是合法的序列, 但是“B-Person I-Organization” 是非法标签序列.\n\nIII：标签序列“O I-label” 是非法的.实体标签的首个标签应该是 “B-“ ，而非 “I-“, 换句话说,有效的标签序列应该是“O B-label”。\n\n有了这些约束，标签序列预测中非法序列出现的概率将会大大降低。\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flvjianxin%2FKnowledge-extraction","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flvjianxin%2FKnowledge-extraction","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flvjianxin%2FKnowledge-extraction/lists"}