Sentence-BERT 句子语义匹配模型的tensorflow实现以及训练trick
论文模型回顾
论文链接:https://arxiv.org/abs/1908.10084
文章在已有的语义匹配模型的基础上提出了基于Bert的句义匹配孪生网络

模型介绍:将两个句子通过Bert(注意:在对句子相似度建模时,两个句子经过的Bert层应该是共享权重的,及同一个Bert)进行特征提取后,取最后一层的hidde_layers进行pooling,文章试验了直接取CLS向量、max_pooling、mean_pooling,结果显示mean_pooling效果最好。将pooling后得到的两个句子向量进行特征交叉,文章尝试了多种交叉方式,|u-v|的效果最好,当然使用者可以根据具体任务和场景自行尝试多种交叉方法;最后通过softmax层。
训练好模型之后,我们可以将语料库中的句子通过单塔转化为对应的句子向量,当待匹配句子进入时,通过向量相似度检索来直接搜索相似句子,节省了大量的模型推理时间。


建模与训练
tensorflow 2.0.0
transformers 3.1.0
模型代码部分
class BertNerModel(tf.keras.Model):
dense_layer = 512
class_num = 2
drop_out_rate = 0.5
def __init__(self,pretrained_path,config,*inputs,**kwargs):
super(BertNerModel,self).__init__()
config.output_hidden_states = True
self.bert = TFBertModel.from_pretrained(pretrained_path,config=config,from_pt=True)
self.liner_layer = tf.keras.layers.Dense(self.dense_layer,activation='relu')
self.softmax = tf.keras.layers.Dense(self.class_num,activation='softmax')
self.drop_out = tf.keras.layers.Dropout(self.drop_out_rate)
def call(self,input_1):
hidden_states_1,_,_ = self.bert((input_1['input_ids'],input_1['token_type_ids'],input_1['attention_mask']))
hidden_states_2,_,_ = self.bert((input_1['input_ids_2'],input_1['token_type_ids_2'
本文介绍了如何基于Sentence-BERT实现句子语义匹配模型,探讨了模型结构、数据处理、训练技巧,包括warm up学习率和focal loss,并分享了在LCQMC数据集上的实验效果和对模型应用的思考。
最低0.47元/天 解锁文章
【NLP论文复现】Sentence-BERT 句子语义匹配模型的tensorflow实现以及训练Trick&spm=1001.2101.3001.5002&articleId=109469031&d=1&t=3&u=8b26265d787a4534b9d52a31b7623a1b)
1306

被折叠的 条评论
为什么被折叠?



