便宜網(wǎng)站建設(shè)價(jià)格微信引流推廣精準(zhǔn)粉
模型介紹
本項(xiàng)目是使用Bert模型來進(jìn)行文本的實(shí)體識(shí)別。
Bert模型介紹可以查看這篇文章:nlp系列(2)文本分類(Bert)pytorch_bert文本分類_牧子川的博客-CSDN博客
模型結(jié)構(gòu)
Bert模型的模型結(jié)構(gòu):
數(shù)據(jù)介紹
數(shù)據(jù)網(wǎng)址:??????https://github.com/buppt//raw/master/data/people-relation/train.txt
實(shí)體1? 實(shí)體2? 關(guān)系 文本
input_ids_list, token_type_ids_list, attention_mask_list, e1_masks_list, e2_masks_list, labels_list = [], [], [], [], [], []for instance in batch_data:# 按照batch中的最大數(shù)據(jù)長(zhǎng)度,對(duì)數(shù)據(jù)進(jìn)行padding填充input_ids_temp = instance["input_ids"]token_type_ids_temp = instance["token_type_ids"]attention_mask_temp = instance["attention_mask"]e1_masks_temp = instance["e1_masks"]e2_masks_temp = instance["e2_masks"]labels_temp = instance["labels"]# 添加到對(duì)應(yīng)的list中input_ids_list.append(torch.tensor(input_ids_temp, dtype=torch.long))token_type_ids_list.append(torch.tensor(token_type_ids_temp, dtype=torch.long))attention_mask_list.append(torch.tensor(attention_mask_temp, dtype=torch.long))e1_masks_list.append(torch.tensor(e1_masks_temp, dtype=torch.long))e2_masks_list.append(torch.tensor(e2_masks_temp, dtype=torch.long))labels_list.append(labels_temp)# 使用pad_sequence函數(shù),會(huì)將list中所有的tensor進(jìn)行長(zhǎng)度補(bǔ)全,補(bǔ)全到一個(gè)batch數(shù)據(jù)中的最大長(zhǎng)度,補(bǔ)全元素為padding_valuereturn {"input_ids": pad_sequence(input_ids_list, batch_first=True, padding_value=0),"token_type_ids": pad_sequence(token_type_ids_list, batch_first=True, padding_value=0),"attention_mask": pad_sequence(attention_mask_list, batch_first=True, padding_value=0),"e1_masks": pad_sequence(e1_masks_list, batch_first=True, padding_value=0),"e2_masks": pad_sequence(e2_masks_list, batch_first=True, padding_value=0),"labels": torch.tensor(labels_list, dtype=torch.long)}
模型準(zhǔn)備
def forward(self, token_ids, token_type_ids, attention_mask, e1_mask, e2_mask):sequence_output, pooled_output = self.bert_model(input_ids=token_ids, token_type_ids=token_type_ids,attention_mask=attention_mask, return_dict=False)# 每個(gè)實(shí)體的所有token向量的平均值e1_h = self.entity_average(sequence_output, e1_mask)e2_h = self.entity_average(sequence_output, e2_mask)e1_h = self.activation(self.dense(e1_h))e2_h = self.activation(self.dense(e2_h))# [cls] + 實(shí)體1 + 實(shí)體2concat_h = torch.cat([pooled_output, e1_h, e2_h], dim=-1)concat_h = self.norm(concat_h)logits = self.hidden2tag(self.drop(concat_h))return logits
模型預(yù)測(cè)
輸入中文句子:丁一嵐與丈夫鄧拓
句子中的實(shí)體1:丁一嵐
句子中的實(shí)體2:鄧拓
在丁一嵐與丈夫鄧拓中丁一嵐與鄧拓的關(guān)系為:夫妻
輸入中文句子:丁一嵐與丈夫鄧拓
句子中的實(shí)體1:鄧拓
句子中的實(shí)體2:丁一嵐
在【丁一嵐與丈夫鄧拓】中【鄧拓】與【丁一嵐】的關(guān)系為:夫妻
輸入中文句子:京德云社演出相聲,演員包括郭德綱、于謙、李菁、高峰、何云偉、曹云金、劉云天、欒云平、岳云鵬等,段子包括《兵器譜》、《大西廂》、《夢(mèng)中婚
句子中的實(shí)體1:郭德綱
句子中的實(shí)體2:劉云天
在【京德云社演出相聲,演員包括郭德綱、于謙、李菁、高峰、何云偉、曹云金、劉云天、欒云平、岳云鵬等,段子包括《兵器譜》、《大西廂》、《夢(mèng)中婚】中【郭德綱】與【劉云天】的關(guān)系為:師生
輸入中文句子:在榮國(guó)府里,雖然官爵是由賈政承繼,但真正主持家政的卻是賈赦這一派,而且賈赦在賈母面前似乎并不得寵。
句子中的實(shí)體1:賈母
句子中的實(shí)體2:賈赦
在【在榮國(guó)府里,雖然官爵是由賈政承繼,但真正主持家政的卻是賈赦這一派,而且賈赦在賈母面前似乎并不得寵。】中【賈母】與【賈赦】的關(guān)系為:父母
源碼獲取
???????Bert 關(guān)系識(shí)別https://github.com/mzc421/Pytorch-NLP/tree/master/12-Bert%20%E5%85%B3%E7%B3%BB%E8%AF%86%E5%88%AB???????
硬性的標(biāo)準(zhǔn)其實(shí)限制不了無限可能的我們,所以啊!少年們加油吧!