nlp series (7) triplet recognition (Bi-LSTM+CRF) pytorch

Model introduction

In entity recognition: using Bert model, CRF model

In relation recognition: using the output of the Bert model and the entity mask, a series of changes are made to obtain the relation

For an introduction to the Bert model, you can view this article: nlp series (2) text classification (Bert) pytorch_bert text classification_Mu Zichuan's Blog-CSDN Blog

For an introduction to the CRF model, you can view this article:

nlp series (6) text entity recognition (Bi-LSTM+CRF) pytorch - Programmer Sought

model structure

draw a simple diagram

Data introduction

Data URL: https://github.com/buppt//raw/master/data/people-relation/train.txt icon-default.png?t=N6B9https://github.com/buppt//raw/master/data/ people-relation/train.txt

Entity 1 Entity 2 Relationship Text

model preparation

The processed data is calculated by the bert model to obtain the weights of the two entities, and then sent to the CRF model to calculate the loss of the two entities, and then refer to the mask of the Bert model, and the output of the bert and the entity mask and its calculation are obtained to obtain the relationship Loss, add the two losses to get the total loss of the model.

    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

model prediction

Text: 
In addition to performing arts, Li Bingbing is enthusiastic about public welfare, initiates and personally participates in a number of environmental charity activities, actively participates in them, and takes the responsibility of giving back to the 
society 
. Bao Qingtian - Starring 
- 
Li 
Bingbing , born in 1907, Sanyuan, Shaanxi, Han nationality, the Communist Party of China, served as the first company commander of the Red Fourth Regiment, and died in 1933 Prediction result 
: 
Ma Zhizhou- Nationality- China 
Ma Zhizhou- 
Date 
of Birth- 1907 - Sanyuan, Shaanxi 

source code acquisition

Bert+CRF triplet identification icon-default.png?t=N6B9https://github.com/mzc421/Pytorch-NLP/tree/master/11-Bert%2BCRF%20%E4%B8%89%E5%85%83%E7%BB%84 %E8%AF%86%E5%88%AB Rigid standards can't actually limit us with infinite possibilities, so ah! Come on guys!

Guess you like

Origin blog.csdn.net/qq_48764574/article/details/132344244