模型介紹
在實體識別中:使用了Bert模型,CRF模型
在關(guān)系識別中:使用了Bert模型的輸出與實體掩碼,進行一系列變化,得到關(guān)系
Bert模型介紹可以查看這篇文章:NLP系列(2)文本分類(Bert)pytorch_bert文本分類-CSDN博客
CRF模型介紹可以查看這篇文章:NLP系列(6)文本實體識別(Bi-LSTM+CRF)pytorch_bi-lstm crf_牧子川的博客-CSDN博客
模型結(jié)構(gòu)
畫了一個簡易圖
數(shù)據(jù)介紹
數(shù)據(jù)網(wǎng)址:??????https://github.com/buppt//raw/master/data/people-relation/train.txthttps://github.com/buppt//raw/master/data/people-relation/train.txt
實體1? 實體2? 關(guān)系 文本
模型準(zhǔn)備
將處理的數(shù)據(jù)通過bert模型計算得到兩個實體的權(quán)重,然后送入到CRF模型計算兩個實體損失,然后借鑒Bert模型的掩碼,將bert的輸出與實體掩碼及其計算,得到關(guān)系的損失,將兩個損失相加,得到模型的總損失。文章來源:http://www.zghlxwxcb.cn/news/detail-680761.html
def compute_loss(self, input_ids, attention_mask, tag_ids, sub_mask, obj_mask, labels, real_lengths):
hidden_output, pooled_output = self.get_features(input_ids, attention_mask)
feats = self.hidden2tag(hidden_output)
total_scores = self.get_total_scores(feats, real_lengths)
gold_score = self.get_golden_scores(feats, tag_ids, real_lengths)
ner_loss = torch.mean(total_scores - gold_score)
relation_logits = self.get_relation_logit(pooled_output, hidden_output, sub_mask, obj_mask)
relation_loss = self.criterion(relation_logits, labels)
return ner_loss + relation_loss
模型預(yù)測
文本: 除演藝事業(yè)外,李冰冰熱心公益,發(fā)起并親自參與多項環(huán)保慈善活動,積極投身其中,身體力行擔(dān)起了回饋社會的責(zé)任于02年出演《少年包青天》,進入大家視線 預(yù)測結(jié)果: 少年包青天 - 主演 - 李冰冰 ========================================= 文本: 馬志舟,1907年出生,陜西三原人,漢族,中國共產(chǎn)黨,任紅四團第一連連長,1933年逝世 預(yù)測結(jié)果: 馬志舟 - 國籍 - 中國 馬志舟 - 出生日期 - 1907年 馬志舟 - 民族 - 漢族 馬志舟 - 出生地 - 陜西三原?
源碼獲取
Bert+CRF 三元組識別https://github.com/mzc421/Pytorch-NLP/tree/master/13-Bert%2BCRF%20%E4%B8%89%E5%85%83%E7%BB%84%E8%AF%86%E5%88%AB硬性的標(biāo)準(zhǔn)其實限制不了無限可能的我們,所以啊!少年們加油吧!文章來源地址http://www.zghlxwxcb.cn/news/detail-680761.html
到了這里,關(guān)于nlp系列(7)三元組識別(Bert+CRF)pytorch的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!